This chapter describes several useful AWK commands and their appropriate 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
You can instruct AWK to print only certain columns from the input field. The following example demonstrates this −
[jerry]$ awk '{print $3 "\t" $4}' marks.txt
On executing this code, you get the following result −
Physics 80 Maths 90 Biology 87 English 85 History 89
In the file marks.txt, the third column contains the subject name and the fourth column contains the marks obtained in a particular subject. Let us print these two columns using AWK print command. In the above example, $3 and $4 represent the third and the fourth fields respectively from the input record.
By default, AWK prints all the lines that match pattern.
[jerry]$ awk '/a/ {print $0}' marks.txt
On executing this code, you get the following result −
2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar English 85 5) Hari History 89
In the above example, we are searching form pattern a. When a pattern match succeeds, it executes a command from the body block. In the absence of a body block − default action is taken which is print the record. Hence, the following command produces the same result −
[jerry]$ awk '/a/' marks.txt
When a pattern match succeeds, AWK prints the entire record by default. But you can instruct AWK to print only certain fields. For instance, the following example prints the third and fourth field when a pattern match succeeds.
[jerry]$ awk '/a/ {print $3 "\t" $4}' marks.txt
On executing this code, you get the following result −
Maths 90 Biology 87 English 85 History 89
You can print columns in any order. For instance, the following example prints the fourth column followed by the third column.
[jerry]$ awk '/a/ {print $4 "\t" $3}' marks.txt
On executing the above code, you get the following result −
90 Maths 87 Biology 85 English 89 History
Let us see an example where you can count and print the number of lines for which a pattern match succeeded.
[jerry]$ awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt
On executing this code, you get the following result −
Count = 4
In this example, we increment the value of counter when a pattern match succeeds and we print this value in the END block. Note that unlike other programming languages, there is no need to declare a variable before using it.
Let us print only those lines that contain more than 18 characters.
[jerry]$ awk 'length($0) > 18' marks.txt
On executing this code, you get the following result −
3) Shyam Biology 87 4) Kedar English 85
AWK provides 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, default action is taken, i.e., the print action. Hence, if a line has more than 18 characters, then the comparison results true and the line gets printed.