$ echo -e "1 \t 2 \t 3 \t 4 \t 5" ←Prints the numbers 1 to 5 in 5 fields 1 2 3 4 5 $ echo -e "1 \t 2 \t 3 \t 4 \t 5" | awk '{print $2,$4}← After processing by AWK, only the second and fourth fields are printed 2 4 2 4 |
The awk '{print $#}' (where # is a number) is the most basic awk command, which prints the specified field. awk can also be used in a more complex way, as shown in the following examples:
awk has many built-in variables, such as "NR" which is the number of lines read, and "NF" which is the number of fields in the current line.
Example:$ echo 'aa bb cc dd' | awk '{print NF}' ← Prints the number of fields in the text displayed on the screen 4 $ awk 'END{print NR}' FILE ← Counts the number of lines in a file (the same as the "wc -l" command) $ awk 'NR <= 5' FILE ←輸rints the first five lines of a file (the same as the "head -n5 FILE" command) $ awk 'NR' FILE ←Removing whitespace-only lines $ seq 1 100 | awk 'NR>=1 && NR < 8' ←Lists the lines in a range |