Awk Command in Linux with Examples
This lesson presents various necessary AWK commands and their related examples.
Consider a text file marks.txt to be processed with the following content ?
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
Printing Column or Field
You may command AWK to print just specified columns from the input field. The following example proves this
Example
[jerry]
$ awk '{print $3 "\t" $4}' marks.txt
On running this code, you get the following result
Output
Physics 80
Maths 90
Biology 87
English 85
History 89
In the file marks.txt, the third column carries the topic name, and the fourth column comprises the marks received in a given subject. Let us print these two columns using the AWK print command. In the given example, $3 and $4 represent the third and the fourth fields, respectively, from the input record.
Printing All Lines
By default, AWK prints all the lines that match the pattern.
Example
[jerry]
$ awk '/a/ {print $0}' marks.txt
On running this code, you get the following result ?
Output
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
In the given an example, we are seeking form pattern a. When a pattern match succeeds, it performs commands from the body block. In the absence of a body block, the default action is executed to print the record. Hence, the following command yields the same result :
Example
[jerry]
$ awk '/a/' marks.txt
Printing Columns by Pattern
When a pattern match succeeds, AWK prints the complete record by default. But you may command AWK to print just particular fields. For instance, the following example publishes the third and fourth fields when a pattern match succeeds.
Example
[jerry]
$ awk '/a/ {print $3 "\t" $4}' marks.txt
On running this code, you get the following result ?
Output
Maths 90
Biology 87
English 85
History 89
Printing Column in Any Order
You may print columns in any sequence. For instance, the following example prints the fourth column followed by the third column.
Example
[jerry]
$ awk '/a/ {print $4 "\t" $3}' marks.txt
On running the code above, you get the following result ?
Output
90 Maths
87 Biology
85 English
89 History
Counting and Printing Matched Pattern
Let us explore an example where you may count and report the number of lines for which a pattern match succeeded.
Example
[jerry]
$ awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt
On running this code, you get the following result ?
Output
Count = 4
In this example, we increase the counter's value when a pattern match succeeds, printing this number in the END block. Note that there is no requirement to define a variable before using it, unlike other programming languages.
Printing Lines with More than 18 Characters
Let us publish just those lines that have more than 18 characters.
Example
[jerry]
$ awk 'length($0) > 18' marks.txt
On running this code, you get the following result ?
Output
3) Shyam Biology 87
4) Kedar English 85
Conclusion
AWK includes a built-in length function that returns the length of the string. $0 variable stores the entire line, and in the absence of a body block, the default action is taken, i.e., the print action. Hence, if a line contains more than 18 characters, the comparison returns true, and the line is printed.