Linux三剑客awk数组、判断、循环

网友投稿 309 2022-11-01


Linux三剑客awk数组、判断、循环

awk数组

数组应用场景

数组的下标可以是任意形式,理解为高级语言的map表即可

统计次数:统计每个用户ip出现的次数,统计每种状态码出现次数,统计系统中每个用户被攻击的次数,统计攻击者ip出现的次数

求和:统计每个ip消耗的流量

shell数组

awk数组

形式

array[0]=oldboy array[1]=lidao

array[0]=oldboy array[1]=lidao

使用

echo ${array[0]} ${array[1]}

print array[0] array[1]

批量输出数组的内容

for i in ${array[*]}

do

echo $i

done

for(i in array)

awk批量输出数组内容

awk 'BEGIN{a[0]=12306; a[1]="lidao"; a[2]="hello"; a[3]=123; for(i in a) print i,a[i]};'

统计下面每个URL出现的次数

-vFS=/ '{array[$3]++;}END{for (url in array) print url, array[url]}' url.txtawk -vFS=/ '{array[$3]++;}END{for (url in array) print url, array[url]}' url.txt | sort

awk '$9~[0-9][0-9][0-9]/{array[$9]++}END{for (i in array) print i, array[i]}' access.log | sort

awk循环

shell循环

awk循环

使用

for(i=1;i<=10;i++)

do

echo $i

done

for(i=1;i<=10;i++) {print i}

1-100求和

awk 'BEGIN{for(i=1;i<=100;i++){sum+=i;} print sum;}'

awk判断

磁盘利用率大于20%,则显示"disk not enough"

df -h | awk -F"[ %]+" 'NR>1{if($5>20){print "disk not enough", $1, $5,$NF}}'

awk使用条件判断时,第一个条件在{动作}前,后面还有条件判断用if

统计一段文本中,单词长度小于6的单词数量

Studying a subject that you feel pointless is never a fun or easy task. If you're study history, asking yourself the question "why is history important" is a very good first step. History is an essential part of human civilization. You will find something here that will arouse your interest, or get you thinking about the significance of history.

shen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){print $i}}' en.txt StudyingasubjectthatyoufeelpointlessisneverafunoreasytaskIfyou'restudyhistoryaskingyourselfthequestionwhyishistoryimportantisaverygoodfirststepHistoryisanessentialpartofhumancivilizationYouwillfindsomethingherethatwillarouseyourinterestorgetyouthinkingaboutthesignificanceofhistoryshen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){if(length($i)>6)print $i}}' en.txt StudyingsubjectpointlesshistoryyourselfquestionhistoryimportantHistoryessentialcivilizationsomethinginterestthinkingsignificancehistoryshen@ubuntu-vm:~/code/test$ awk -F"[ .\",]+" '{for(i=1;i<=NF;i++){if(length($i)>6) sum+=1}} END{print sum}' en.txt 16


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MySQL 配置主从复制实践
下一篇:Redis set和sorted_set数据类型
相关文章

 发表评论

暂时没有评论,来抢沙发吧~