Skip to content

Latest commit

 

History

History
111 lines (68 loc) · 2.98 KB

02.grep, sed, and awk for pattern matching and text processing.md

File metadata and controls

111 lines (68 loc) · 2.98 KB

Pattern Matching and Text Processing with grep, sed, and awk in Linux

Pattern matching and text processing are common tasks in Linux scripting. Commands like grep, sed, and awk are powerful tools for handling these tasks efficiently. This tutorial will guide you through the usage of these commands in a Linux environment.

grep - Global Regular Expression Print

Basic Usage

The grep command is widely used for searching patterns in files. The basic syntax is:

grep PATTERN FILE

For example, to search for the word "error" in a file:

grep "error" logfile.txt

Options

  • -i: Ignore case.
  • -r: Recursively search subdirectories.
  • -n: Show line numbers.
  • -v: Invert match, i.e., show lines not matching the pattern.
grep -i -n "warning" -r /path/to/directory

sed - Stream Editor

Basic Usage

The sed command is a stream editor that can perform basic text transformations. The basic syntax is:

sed 's/old_text/new_text/g' filename.txt

For example, to replace "apple" with "orange" in a file:

sed 's/apple/orange/g' fruits.txt

Options

  • -i: Edit files in place.
  • -e: Specify multiple commands.
sed -i 's/old/new/g' file.txt

awk - Text Processing Tool

Basic Usage

The awk command is a versatile tool for text processing. The basic syntax is:

awk 'PATTERN { COMMAND }' FILE

For example, to print the second and fourth columns of a file:

awk '{ print $2, $4 }' data.txt

Options

  • -F: Specify a field separator.
awk -F':' '{ print $1 }' /etc/passwd

Combining grep, sed, and awk

Example Scenario

Let's say you have a log file and you want to find all lines containing the word "error," replace "error" with "warning," and then print the timestamp and error message.

grep "error" logfile.txt | sed 's/error/warning/g' | awk '{print $1, $2, $3, $4, $5}'

In this example, grep filters lines with "error," sed replaces "error" with "warning," and awk extracts the timestamp and error message columns.

Practical Example - Extracting IP Addresses

Let's use grep, sed, and awk to extract IP addresses from a log file:

grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" access.log | sed 's/\./-/g' | awk '{print "IP Address: " $1}'

This example combines grep with a regular expression to find IP addresses, sed to replace dots with hyphens, and awk to format the output.

Conclusion

Mastering grep, sed, and awk is essential for effective pattern matching and text processing in Linux scripting. Whether you are searching for specific patterns, replacing text, or extracting information from files, these commands provide powerful solutions. Experiment with different options and combinations to tailor these tools to your specific scripting needs.