awk is a more powerful text-processing tool than SED, where sed is usually treated as an entire unit of action, while awk can separate a whole line into multiple fields by the specified delimiter.
The syntax of awk
Awk-f separator ' Begin{command}condition1{command1}condition2{command2} ... End{command} ' filename #除了处理filename之外 awk also handles standard input
- f Specifies the delimiter, which by default is a space character, for example, if you specify: As a delimiter, then-f:
Begin is used to specify the instructions that should be executed before the first line is processed, and can be separated by semicolons if the command is multiple
End is used to specify the instructions that should be executed after all lines of text have been processed
The Middle Condition{command} is used to specify the instructions that should be executed when the line of text is processed, condition is used to specify that the condition is usually an expression, and the command is used to specify what should be done when the condition is satisfied.
Several common built-in variables for awk
$ A full line of content currently being processed
$ The currently processed row uses the first field after the separator separation
..... The second field
$# ..... And so on
NR line number currently being processed
NF the number of fields currently processed by the line
FS currently specifies a space character, the default is the spacebar can actually be replaced with the-f option
awk uses print or printf to print eligible content to the screen, and there are some areas
With print, the fields are used, separated, and when printed, the fields are separated by spaces.
An example
Awk-f: ' {print $1,$3} '/etc/passwd
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/85/73/wKiom1ej7ECSgYA5AAA4ZXWu8XM386.jpg-wh_500x0-wm_3 -wmp_4-s_4074002256.jpg "title=" newscreen4512094.jpg "alt=" Wkiom1ej7ecsgya5aaa4zxwu8xm386.jpg-wh_50 "/>
If you want to specify a format, you can use "" to add a transfer character such as "\ T"
Awk-f: ' {print $ \ t ' $ '/etc/passwd
In contrast, printf is more commonly used, and printf makes it easy to print content in a specified format
printf Basic format printf "pattern" content
There are a few points to note when using printf in awk
Use between 1.pattern and content, separate
2. Do not use $var_name (except for built-in variables such as $0,$1) when printing variable content using printf
3. The variables to be printed are also used, separate
4. You need to manually specify a newline character in the pattern \ n
Awk-f: ' {printf '%10s\t%10s\n '},$1,$3 '/etc/passwd
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/85/73/wKioL1ej72CR6mYQAAA49EXOCA0004.jpg-wh_500x0-wm_3 -wmp_4-s_3284053431.jpg "title=" newscreen5327434.jpg "alt=" Wkiol1ej72cr6myqaaa49exoca0004.jpg-wh_50 "/>
awk features much more than that, and here are some of the other features that are commonly used by awk
1. Using the arithmetic operator
Common arithmetic operations can be performed in Awk's command
Common arithmetic operators by +-*/% + = =%=/= + +--
2. Using comparison operators
> < >= <= = = = = ~
It is worth mentioning that to determine whether a field conforms to the regular expression pattern, the pattern of the regular expression needs to be enclosed in//.
Example
Awk-f: ' $NF ~/nologin$/{print $, $NF} '/etc/passwd
3. Using Control statements
If Else statement
Awk-f: ' {if ($3<499) {print $, ' System user '}else{print $, ' Common user '}} '/etc/passwd
Note that two points, unlike Condition{command}, if the condition statement is written in {}, if the execution of the statement by more than one, you must use {}, if the execution of a statement, you can omit {}
4. Using Loop statements
While
Awk-f: ' {i=1;while (i<=3) {printf "%-10s", $i; i++} '/etc/passwd #对于每行只打印前三个字段
For
Awk-f: ' {for (i=1;i<=3;i++) {print $i}} '/etc/passwd
Use for can also be used to iterate over an array
The syntax structure is for the for (var in array) {command} #注意var是下标, not the contents of the array
Awk-f: ' {shell[$NF]++}end{for (S in SHELL) {print S,shell[s]}} '/etc/passwd #统计每种shell用户的数量
5.continue,break,case,next is not very common here.
Of course, awk is much more than that, and it's just a list of some of the most common features that you'll want to know about yourself.
This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1834647
Linux Basics (20) Text processing the Three Musketeers of awk