linux 三剑客之awk总结

AWK

1.begin end使用

cat /tmp/passwd |awk -F ':' 'BEGIN {print "hello"} {print $1"\t"$3} END {print "ending"}'

2.匹配

[root@zeus tmp]# cat /tmp/passwd |awk -F ':' 'BEGIN {print "hello"} /root/{print $1"\t"$3} END {print $2}'     

hello

root    0

operator        11

x

3.printf

[root@zeus tmp]# cat /tmp/passwd |awk -F ':' '/root/{printf("%s %s %s\n",NR,$1,$2)}'

1 root x

10 operator x

4.变量:

[root@zeus tmp]# ls -l|awk 'BEGIN {row=0;} {row++;} END {printf("total:\t%s\n",row)}'

total:  24

5.判断

[root@zeus tmp]# ls -l|awk 'BEGIN {row=0;} {print $3;if($3=="root"){row++;}} END {printf("total:\t%s\n",row)}'

6.循环

awk -F ':' 'BEGIN {count=0;} {name[count] = $1;count++;}; END{for (i = 0; i < NR; i++) print i, name[i]}' /etc/passwd

posted @ 2019-06-21 09:45  朤尧  阅读(477)  评论(0编辑  收藏  举报