一、匹配代码块

匹配模式如下:

1//纯字符匹配
2!//纯字符不匹配
3~//字段值匹配
4!~//字段值不匹配
5~/a1|a2/字段值匹配a1或a2

示例如下:

 1awk '/mysql/' /etc/passwd
 2awk '/mysql/{print }' /etc/passwd
 3awk '/mysql/{print $0}' /etc/passwd                   //三条指令结果一样
 4awk '!/mysql/{print $0}' /etc/passwd                  //输出不匹配mysql的行
 5awk '/mysql|mail/{print}' /etc/passwd
 6awk '!/mysql|mail/{print}' /etc/passwd
 7awk -F: '/mail/,/mysql/{print}' /etc/passwd         //区间匹配
 8awk '/[2][7][7]*/{print $0}' /etc/passwd       //匹配包含27为数字开头的行,如27,277,2777...
 9awk -F: '$1~/mail/{print $1}' /etc/passwd           //$1匹配指定内容才显示
10awk -F: '{if($1~/mail/) print $1}' /etc/passwd     //与上面相同
11awk -F: '$1!~/mail/{print $1}' /etc/passwd          //不匹配
12awk -F: '$1!~/mail|mysql/{print $1}' /etc/passwd

二、IF语句

必须用在{}中,且比较内容用()扩起来

1awk -F: '{if($1~/mail/) print $1}' /etc/passwd            //简写
2awk -F: '{if($1~/mail/) {print $1}}'  /etc/passwd         //全写
3awk -F: '{if($1~/mail/) {print $1} else {print $2}}' /etc/passwd     //if...else...

三、条件表达式

表达式:

1==   !=   >   >=

示例:

1awk -F":" '$1=="mysql"{print $3}' /etc/passwd
2awk -F":" '{if($1=="mysql") print $3}' /etc/passwd          //与上面相同
3awk -F":" '$1!="mysql"{print $3}' /etc/passwd                 //不等于
4awk -F":" '$3>1000{print $3}' /etc/passwd                      //大于
5awk -F":" '$3>=100{print $3}' /etc/passwd                     //大于等于
6awk -F":" '$3<1{print $3}' /etc/passwd                            //小于
7awk -F":" '$3<=1{print $3}' /etc/passwd                         //小于等于

四、逻辑运算符

表达式:

1&& ||

示例

1awk -F: '$1~/mail/ && $3>8 {print }' /etc/passwd         //逻辑与,$1匹配mail,并且$3>8
2awk -F: '{if($1~/mail/ && $3>8) print }' /etc/passwd
3awk -F: '$1~/mail/ || $3>1000 {print }' /etc/passwd       //逻辑或
4awk -F: '{if($1~/mail/ || $3>1000) print }' /etc/passwd

五、数值运算

1awk -F: '$3 > 100' /etc/passwd
2awk -F: '$3 > 100 || $3 < 5' /etc/passwd
3awk -F: '$3+$4 > 200' /etc/passwd
4awk -F: '/mysql|mail/{print $3+10}' /etc/passwd        //第三个字段加10打印
5awk -F: '/mysql/{print $3-$4}' /etc/passwd             //减法
6awk -F: '/mysql/{print $3*$4}' /etc/passwd             //求乘积
7awk '/MemFree/{print $2/1024}' /proc/meminfo           //除法
8awk '/MemFree/{print int($2/1024)}' /proc/meminfo      //取整

六、输出分隔符OFS

1awk '$6 ~ /FIN/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt
2awk '$6 ~ /WAIT/ || NR==1 {print NR,$4,$5,$6}' OFS="\t" netstat.txt
3//输出字段6匹配WAIT的行,其中输出每行行号,字段4,5,6,并使用制表符分割字段

七、输出处理结果到文件

1①在命令代码块中直接输出    route -n|awk 'NR!=1{print > "./fs"}'
2②使用重定向进行输出           route -n|awk 'NR!=1{print}'  > ./fs

八、格式化输出

1netstat -anp|awk '{printf "%-8s %-8s %-10s\n",$1,$2,$3}'

printf表示格式输出

  • %格式化输出分隔符
  • -8长度为8个字符
  • s表示字符串类型

打印每行前三个字段,指定第一个字段输出字符串类型(长度为8),第二个字段输出字符串类型(长度为8),

第三个字段输出字符串类型(长度为10)

1netstat -anp|awk '$6=="LISTEN" || NR==1 {printf "%-10s %-10s %-10s \n",$1,$2,$3}'
2netstat -anp|awk '$6=="LISTEN" || NR==1 {printf "%-3s %-10s %-10s %-10s \n",NR,$1,$2,$3}'

IF语句

 1awk -F: '{if($3>100) print "large"; else print "small"}' /etc/passwd
 2small
 3small
 4small
 5large
 6small
 7small
 8awk -F: 'BEGIN{A=0;B=0} {if($3>100) {A++; print "large"} else {B++; print "small"}} END{print A,"\t",B}' /etc/passwd
 9
10//ID大于100,A加1,否则B加1
11awk -F: '{if($3<100) next; else print}' /etc/passwd    //小于100跳过,否则显示
12awk -F: 'BEGIN{i=1} {if(i<NF) print NR,NF,i++ }' /etc/passwd
13awk -F: 'BEGIN{i=1} {if(i<NF) {print NR,NF} i++ }' /etc/passwd
14另一种形式
15awk -F: '{print ($3>100 ? "yes":"no")}'  /etc/passwd
16awk -F: '{print ($3>100 ? $3":\tyes":$3":\tno")}'  /etc/passwd

while语句

1awk -F: 'BEGIN{i=1} {while(i<NF) print NF,$i,i++}' /etc/passwd
27 root 1
37 x 2
47 0 3
57 0 4
67 root 5
77 /root 6

数组

 1netstat -anp|awk 'NR!=1{a[$6]++} END{for (i in a) print i,"\t",a[i]}'
 2netstat -anp|awk 'NR!=1{a[$6]++} END{for (i in a) printf "%-20s %-10s %-5s \n", i,"\t",a[i]}'
 39523                               1
 49929                               1
 5LISTEN                            6
 67903                               1
 73038/cupsd                   1
 87913                               1
 910837                             1
109833                               1

应用1

1awk -F: '{print NF}' helloworld.sh                                                       //输出文件每行有多少字段
2awk -F: '{print $1,$2,$3,$4,$5}' helloworld.sh                 //输出前5个字段
3awk -F: '{print $1,$2,$3,$4,$5}' OFS='\t' helloworld.sh      //输出前5个字段并使用制表符分隔输出
4awk -F: '{print NR,$1,$2,$3,$4,$5}' OFS='\t' helloworld.sh    //制表符分隔输出前5个字段,并打印行号

应用2

1awk -F'[:#]' '{print NF}'  helloworld.sh                                                  //指定多个分隔符: #,输出每行多少字段
2awk -F'[:#]' '{print $1,$2,$3,$4,$5,$6,$7}' OFS='\t' helloworld.sh   //制表符分隔输出多字段

应用3

1awk -F'[:#/]' '{print NF}' helloworld.sh              //指定三个分隔符,并输出每行字段数
2awk -F'[:#/]' '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12}' helloworld.sh     //制表符分隔输出多字段

应用4

计算/home目录下,普通文件的大小,使用KB作为单位

1ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",sum/1024,"KB"}'
2ls -l|awk 'BEGIN{sum=0} !/^d/{sum+=$5} END{print "total size is:",int(sum/1024),"KB"}'         //int是取整的意思

应用5

统计netstat -anp 状态为LISTEN和CONNECT的连接数量分别是多少

1netstat -anp|awk '$6~/LISTEN|CONNECTED/{sum[$6]++} END{for (i in sum) printf "%-10s %-6s %-3s \n", i," ",sum[i]}'

应用6

统计/home目录下不同用户的普通文件的总数是多少?

1ls -l|awk 'NR!=1 && !/^d/{sum[$3]++} END{for (i in sum) printf "%-6s %-5s %-3s \n",i," ",sum[i]}'
2mysql        199
3root           374

统计/home目录下不同用户的普通文件的大小总size是多少?

1ls -l|awk 'NR!=1 && !/^d/{sum[$3]+=$5} END{for (i in sum) printf "%-6s %-5s %-3s %-2s \n",i," ",sum[i]/1024/1024,"MB"}'

应用7

输出成绩表

 1awk 'BEGIN{math=0;eng=0;com=0;printf "Lineno.   Name    No.  
 2  Math   English   Computer    Total\n";printf
 3"------------------------------------------------------------\n"}{math+=$3;
 4 eng+=$4; com+=$5;printf "%-8s %-7s %-7s %-7s %-9s %-10s %-7s
 5\n",NR,$1,$2,$3,$4,$5,$3+$4+$5} END{printf
 6"------------------------------------------------------------\n";printf
 7"%-24s %-7s %-9s %-20s \n","Total:",math,eng,com;printf "%-24s %-7s %-9s
 8 %-20s \n","Avg:",math/NR,eng/NR,com/NR}' test0
 9
10[root@localhost home]# cat test0
11Marry   2143 78 84 77
12Jack    2321 66 78 45
13Tom     2122 48 77 71
14Mike    2537 87 97 95
15Bob     2415 40 57 62

awk-avg-printf
awk-avg-printf