Awk is also a streaming editor. It operates on lines in the document and executes line by line. Awk has all the functions of sed , and it is more powerful. The awk tool is actually very complicated (there is a special book to introduce its application, If you are interested, you can learn more about it).
Reference: http://www.linuxidc.com/Linux/2014-12/110939.htm
1. , intercept a segment in the document
Command: head -n2 passwd | awk -F ':' '{print $1}'
The function of the -F option is to specify the delimiter. If no -F is specified, the space or tab is used as the delimiter. Print is the action of printing, which is used to print out a field. 1 is the first field, 2 is the second field, and so on,
1.1 has a special that is $0 , which means the entire line
Command: head -n2 passwd | awk -F ':' '{print $0}'
1.2 Pay attention to the format of awk , -F is followed by a single quotation mark, and then there is a separator inside. The action of print should be enclosed in { } , otherwise an error will be reported. print can also print custom content, but the custom content should be enclosed in double quotes .
命令: head -n2 passwd | awk -F ':' '{print $1"#"$2"#"$3"#"}'
2. Match characters or strings
This is similar to the usage of sed . It can realize the function of grep , but there is no color display . It is definitely not as convenient as grep .
2.1 But awk has more powerful matching than sed , let a certain segment match , where '~' means matching
Command : awk -F ':' '$1 ~/oo/' passwd
2.2 awk can also match multiple times
Command: awk -F ':' '/root/ {print $1,$3} /test/ {print $1,$3,$4}' passwd
In the above example, awk matches root , and then matches user , it can also print only the matched segment
2.4 awk also supports regular expressions
Command: awk -F ':' '$3==0' passwd
In awk, it can be judged by logical symbols. For example, '==' means equal, and it can also be understood as 'exact match'. There are also >, '>=, '<, '<=, '!= and so on. It is worth noting Yes, when comparing with numbers, if the numbers to be compared are enclosed in double quotes, then awk will not consider them as numbers, but as characters, and without double quotes, it will be considered as numbers.
3.1 Add double quotes to numbers, command: awk -F ':' '$3>="500"' passwd
In the above example, we wanted to print out the line with uid greater than or equal to 500 , but the result was not what we expected, because awk treats all numbers as characters.
3.2 The sorting principle of sort mentioned in the previous chapter is the same, but if you do not add double quotes , you will get the desired result
命令:awk -F ':' '$3>=500' passwd
3.3 !=表示不匹配,它除了针对某一个段的字符进行逻辑比较外,还可以在两个段之前进行逻辑比较,命令:awk -F ':' '$7!="/sbin/nologin"' passwd
3.5 还可以使用&&(并且)和||(或者),它们分别表示:“并且”和“或者”
3.5.1&&的用法,命令:awk -F ':' '$3>"5" && $3<"7"' passwd
3.5.2 ||(或者)的用法,命令:awk -F ':' '$3>"5" || $7=="/bin/bash"' passwd
4、awk的内置变量,awk常用的变量有OFS、NF、NR,
OFS 和-F选项有类似的功能,也是和来定义分隔符的,但是它是在输出的时候定义,
4.1 OFS用法,命令:awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' passwd
4.2 NF 用法,命令:awk -F ':' '{print NF":"$0}' passwd
4.3 NR的用法,表示多少行,命令:awk -F ':' '{print NR":"$0}' passwd
命令: awk -F ‘:’ ‘NR<20 && $1 ~ /roo/’ /etc/passwd
命令:head -n 3 passwd | awk -F ':' '$1="root"'
5.2 Mathematical operations on the values of each segment
Command: head -n3 /etc/passwd |awk -F ':' ' {$7=$4+$3 ; print $0}'
5.3 Calculate the sum of a segment
Command: awk -F ':' '{(tot=tot+$3)} ; END{print tot}' passwd
Pay attention to the END here , indicating that all lines have been executed . This is a unique syntax of awk. In fact, both awk and sed can be written into a script file, and they have their own unique syntax. Use if judgment and for loop in awk. is allowed.