The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
     

Text Data Processing with "awk"



awk is a text processing tool with its own scripting language. Its syntax is similar to that of the C programming language, but less complex (as it has only around a dozen commands). Additionally, awk supports regular expressions that are not present in C.awkprocesses text in terms of fields, as shown in the following examples:
$ 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

you are not familiar with awk, its functionality is not easy to explain in a few words. However, it is worth spending some time learning it. For more information, please refer to the following link.