While working with text files in Unix-like systems, three commands come in very handy for processing and analyzing data: grep, cut, and awk. These powerful utilities can be combined in countless ways to filter, rearrange, and manipulate text. Let’s dig into some fascinating ways to use them, both individually and together.
1. Chaining grep and cut
grep is used to search text, while cut is used to slice it. We can use them in tandem to refine our search and extract only the information we need. Here’s an example:
grep SEARCHTERM file.txt | grep "NEWSSEARCHTERM" | cut -f1,17- -d" "
This command first uses grep to find lines containing SEARCHTERM. From these lines, it then finds lines containing NEWSSEARCHTERM. Finally, cut is used to extract the 1st and 17th (and onward) fields from these lines, using a space as a delimiter.
And here’s a real-life example:
grep FILENAME_log_2014-09-11 | grep " 500 " | cut -f1,18- -d" " | grep -v "200 -"
This command finds lines containing FILENAME_log_2014-09-11 and 500, excludes those with 200 -, and then extracts the 1st and 18th (and onward) fields.
2. grep Flags and Options
grep offers many useful flags. -P enables Perl-Compatible Regular Expression (PCRE), -A and -B display lines after and before the matched line, respectively, and -C displays lines around the matched line:
grep -P "\[\d+\] (Publish Event:)?(Sent Points)?(Sending Points:)?(Sent Points:)?(Point received)?"
grep -A 5 -B 3 "Hello"
grep -C 3 "Hello"
3. Live Monitoring with tail, grep, and cut
When monitoring log files, you can use tail -f to follow the file’s updates in real time. This can be piped into grep and cut for live filtering and slicing:
tail -f /var/log/server.log | while read line; do echo "$line" | grep -i "no configuration found" | cut -d" " -f8-10,11,13 ; done
4. grep OR and NOT Operators
grep supports logical OR (|) and NOT (-v) operations, which can be combined with -E for extended regular expressions:
grep 'pattern1\|pattern2' filename
grep -E 'pattern1|pattern2' filename
grep -v 'pattern1' filename
5. Using awk
awk is a powerful text processing language that can be used for tasks such as scanning for patterns and performing actions on matched lines. Here’s an example of awk being used to print the third column of a file:
awk '{print $3}' filename
You can also use awk in combination with grep to filter lines before processing them. Here’s an example where we first filter lines containing “Error” and then print the fifth column:
grep "Error" filename | awk '{print $5}'
In conclusion, grep, cut, and awk provide a wide array of functionalities for text processing in Unix-like systems. Mastering these commands and understanding how to combine them will greatly enhance your ability to handle and analyze text data.
Buy Me a Coffee