#yyds干货盘点#awk流程控制条件和循环处理&&高级用法数组和函数

1.条件判断 if-else 语法: if(condition1){statement1}else if(condition2){statement2}else if(condition3) {statement3}......else{statementN} 使用场景:对awk取得的整行或某个字段做条件判断 ​ [root@c7-147 ~]#awk -F: '{if($3>=1000)print $1,$3}' /etc/passwd wang 1000 nfsnobody 65534 [root@c7-147 ~]#awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd root wang [root@c7-147 ~]#awk '{if(NF>5) print $0}' /etc/fstab # Created by anaconda on Thu Jun 10 05:04:06 2021 # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info #/dev/mapper/centos-root / xfs defaults 0 0 UUID=1ebf2de9-d896-401d-9d33-1e74eee806da / xfs defaults 0 0 UUID=af5925a2-c99f-4f03-892b-edafd53060ee /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 [root@c7-147 ~]#awk -F: '{if($3>=1000) {printf "Common user: %s\n",$1}else{printf "root or Sysuser: %s\n",$1}}' /etc/passwd root or Sysuser: root root or Sysuser: bin root or Sysuser: daemon root or Sysuser: adm root or Sysuser: lp root or Sysuser: sync root or Sysuser: shutdown root or Sysuser: halt root or Sysuser: mail root or Sysuser: operator root or Sysuser: games root or Sysuser: ftp root or Sysuser: nobody [root@c7-147 ~]#awk -F: '{if($3>=1000) printf "Common user: %s\n",$1; else printf "root or Sysuser: %s\n",$1}' /etc/passwd root or Sysuser: root root or Sysuser: bin root or Sysuser: daemon root or Sysuser: adm root or Sysuser: lp root or Sysuser: sync root or Sysuser: shutdown root or Sysuser: halt root or Sysuser: mail root or Sysuser: operator root or Sysuser: games root or Sysuser: ftp root or Sysuser: nobody [root@c7-147 ~]#df -h|awk -F% '/^\/dev\/sd/{print $1}'| awk '$NF>=80{print $1,$5}' [root@c7-147 ~]#df | awk -F"[[:space:]]+|%" '/^\/dev\/sd/{if($5>80)print $1,$5}' [root@c7-147 ~]#df | awk -F' +|%' '/^\/dev\/sd/{if($5>=10)print $1,$5}' /dev/sda1 26 [root@c7-147 ~]#awk 'BEGIN{ test=100;if(test>90){print "very good"}else if(test>60){ print "good"}else{print "no pass"}}' very good 2.switch语句 相当于shell里面的case switch(expression) {case VALUE1 or /REGEXP/: statement1; case VALUE2 or /REGEXP2/: statement2; ...; default: statementn} 3. 循环while 语法: while (condition) {statement;…} 条件“真”,进入循环;条件“假”,退出循环 使用场景: 对一行内的多个字段逐一类似处理时使用 对数组中的各元素逐一处理时使用 #内置函数length()返回字符数,而非字节数 [root@centos8 ~]#awk 'BEGIN{print length("hello")}' 5 [root@centos8 ~]#awk 'BEGIN{print length("马哥教育")}' 4 [root@centos7 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF){print $i,length($i); i++}}' /etc/grub2.cfg linux16 7 /vmlinuz-3.10.0-1062.el7.x86_64 31 root=UUID=bebb9244-bbb8-4c69-9249-54a36c75155e 46 ro 2 crashkernel=auto 16 rhgb 4 quiet 5 net.ifnames=0 13 linux16 7 /vmlinuz-0-rescue-b12558570741487c9328c996e3265b09 50 root=UUID=bebb9244-bbb8-4c69-9249-54a36c75155e 46 ro 2 crashkernel=auto 16 rhgb 4 quiet 5 net.ifnames=0 13 [root@centos7 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i=10){print $i,length($i)}; i++}}' /etc/grub2.cfg /vmlinuz-3.10.0-1062.el7.x86_64 31 root=UUID=bebb9244-bbb8-4c69-9249-54a36c75155e 46 crashkernel=auto 16 net.ifnames=0 13 /vmlinuz-0-rescue-b12558570741487c9328c996e3265b09 50 root=UUID=bebb9244-bbb8-4c69-9249-54a36c75155e 46 crashkernel=auto 16 net.ifnames=0 13 #求和 [root@centos8 ~]#awk 'BEGIN{ total=0;i=1;while(i<=100){total+=i;i++};print total}' 5050 计算数据的时间比较 4. 循环 do-while 语法: do {statement;…}while(condition) 意义:无论真假,至少执行一次循环体 do-while循环 语法:do {statement;…}while(condition) 意义:无论真假,至少执行一次循环体 [root@c7-147 ~]#awk 'BEGIN{ total=0;i=1;do{ total+=i;i++;}while(i<=100);print total}' 5050 5. 循环for 语法: for(expr1;expr2;expr3) {statement;…} 常见用法: for(variable assignment;condition;iteration process) {for-body} 特殊用法:能够遍历数组中的元素 for(var in array) {for-body} [root@c7-147 ~]#awk 'BEGIN{total=0;for(i=1;i<=100;i++){total+=i};print total}' 5050 [root@c7-147 ~]#awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}' /etc/grub2.cfg linux16 7 /vmlinuz-3.10.0-327.el7.x86_64 30 root=/dev/mapper/centos-root 28 ro 2 crashkernel=auto 16 rd.lvm.lv=centos/root 21 rd.lvm.lv=centos/swap 21 rhgb 4 quiet 5 net.ifnames=0 13 linux16 7 /vmlinuz-0-rescue-eaf151aad9b94a4da21bea597ac3082a 50 root=/dev/mapper/centos-root 28 ro 2 crashkernel=auto 16 rd.lvm.lv=centos/root 21 rd.lvm.lv=centos/swap 21 rhgb 4 quiet 5 net.ifnames=0 13 6.continue和break continue 中断本次循环 break 中断整个循环 格式: continue [n] break [n] [root@centos8 ~]#awk 'BEGIN{sum=0;for(i=1;i<=100;i++){if(i%2==0)continue;sum+=i}print sum}' 2500 [root@centos8 ~]#awk 'BEGIN{sum=0;for(i=1;i=1000) {system("iptables -A INPUT -s "i" -j REJECT")}}}' nginx.access.log-20200428 system()函数调用 9.多维数组 [root@centos8 ~]#awk 'BEGIN{ >array[1][1]=11 >array[1][2]=12 >array[1][3]=13 >array[2][1]=21 >array[2][2]=22 >array[2][3]=23 >for (i in array) >for (j in array[i]) >print array[i][j] >}' 11 12 13 21 22 23 10.awk函数 awk 的函数分为内置和自定义函数 数值处理: rand():返回0和1之间一个随机数 srand():配合rand() 函数,生成随机数的种子 int():返回整数 [root@centos8 ~]#awk 'BEGIN{srand();print rand()}' 0.790437 [root@centos8 ~]#awk 'BEGIN{srand();print rand()}' 0.283736 [root@centos8 ~]#awk 'BEGIN{srand();print rand()}' 0.948082 [root@centos8 ~]#awk 'BEGIN{srand();print rand()}' 0.371798 [root@centos8 ~]#awk 'BEGIN{srand(); for (i=1;i<=10;i++)print int(rand()*100) }' 35 17 35 95 19 15 70 54 46 93

尚美源码教程库提供精美的网站源码下载,小程序、公众号、H5、APP、游戏、直播、支付、区块链、商城、影音、小说等源码大全。
尚美源码教程库 » #yyds干货盘点#awk流程控制条件和循环处理&&高级用法数组和函数
赞助VIP 享更多特权,立即登录下载海量资源
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡