The awk command performs pattern matching on files. It is based on the AWK programming language. The awk keyword is followed by the pattern, the action to be performed, and the file name. The action to be performed is given within curly braces. The pattern and the action to be performed should be specified within single quotes. If the pattern is not specified, the action is performed on all input data; however, if the action is not specified, the entire line is printed. The awk command can be executed from the command-line or from within an awk script file.
The awk command can be used to process text files in a variety of ways, such as extracting text matching a certain pattern; deleting text matching a certain pattern; adding text matching a certain pattern; and much more.
Syntax
The syntax of the awk command is:
# awk [options] ['patterns {actions}'] {file names}
awk Command Examples
1. Print the fifth column (a.k.a. field) in a space-separated file:
# awk '{print $5}' filename
2. Print the second column of the lines containing “foo” in a space-separated file:
# awk '/foo/ {print $2}' filename
3. Print the last column of each line in a file, using a comma (instead of space) as a field separator:
# awk -F ',' '{print $NF}' filename
4. Sum the values in the first column of a file and print the total:
# awk '{s+=$1} END {print s}' filename
5. Print every third line starting from the first line:
# awk 'NR%3==1' filename
6. Print different values based on conditions:
# awk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' filename
7. Print all lines where the 10th column value equals the specified value:
# awk '($10 == value)'
8. Print all the lines which the 10th column value is between a min and a max:
# awk '($10 >= min_value && $10 <= max_value)'