The awk
command in Linux is a powerful text-processing tool that lets you filter, transform, and report data within text files. The command's effectiveness lies in its ability to use patterns to match specific lines in a file and then execute actions on those lines. This guide will walk you through the fundamentals of awk
patterns and how to use them to process text data.
Introduction to AWK and Its Pattern-Action Model
awk
operates based on a pattern-action pair model. It works as follows:
pattern { action }
Whenever a line in the input file matches the given pattern, awk
will execute the defined action on that line.
Commonly Used AWK Patterns
Regular Expressions:
Enclose regular expressions with slashes /
. To print lines containing "Linux", use:
awk '/Linux/ { print $0 }' filename.txt
Relational Expressions:
Use relational operators like <
, <=
, >
, >=
, ==
, !=
for comparisons. For instance, to print lines where the first field is greater than 100:
awk '$1 > 100 { print $0 }' filename.txt
BEGIN and END Patterns:
The BEGIN
pattern action is executed before any lines are read, while the END
pattern action is executed after all lines have been read. To sum values in the first field and print the total, use:
Using Multiple Patterns with AWK
awk
allows the use of multiple patterns. If any pattern matches, the associated action gets executed. For example:
awk '/pattern1/ || /pattern2/ { print $0 }' filename.txt
Combining Patterns: AND & OR Operations
AND (&&
): To match lines that meet both criteria:
awk '$1 > 10 && $2 ~ /Linux/ { print $0 }' filename.txt
OR (||
): To match lines that meet at least one criterion:
awk '$1 > 10 || $2 ~ /Linux/ { print $0 }' filename.txt
Using AWK's Built-in Variables in Patterns
NR: To print the tenth line of a file:
awk 'NR==10 { print $0 }' filename.txt
NF: To print lines with more than three fields:
awk 'NF > 3 { print $0 }' filename.txt
Conclusion
AWK is a robust text-processing utility in the Linux ecosystem. With its pattern-action model, AWK allows for intricate commands that can filter and transform text data effectively. By understanding the different patterns available in AWK, you can unleash its full power and simplify numerous text-processing tasks.