Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 classic cases of AWK?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article to share with you is about AWK 10 classic cases are what kind of, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

awk is a programming language tool for processing text under Linux system. It can process standard input or files, sort data, calculate and generate reports with short programs. It is widely used.

Basic command syntax: awk option 'pattern {action}' file

The following is a summary of 10 practical awk cases based on work experience. Interview written questions are often given for friends to learn.

1. Analyze access logs (Nginx for example)

Log Format: '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"' Statistics of IP access times: # awk '{a[$1]++}END{for(v in a)print v,a[v]}' access.log Statistics of IP access times greater than 100:# awk '{a[$1]++}END{for(v ina){if(a[v]>100)print v,a[v]}' access.log Count the number of IP visits and sort the top 10:# awk '{a[$1]++}END{for(v in a)print v,a[v]|"sort -k2 -nr |head -10"}'access.log statistics time period most visited IP: # awk'$4>="[02/Jan/2017:00:02:00" &&$4

< ="[02/Jan/2017:00:03:00"{a[$1]++}END{for(v in a)print v,a[v]}'access.log统计上一分钟访问量:# date=$(date -d '-1 minute'+%d/%d/%Y:%H:%M)# awk -vdate=$date '$4~date{c++}END{printc}' access.log统计访问最多的10个页面:# awk '{a[$7]++}END{for(vin a)print v,a[v]|"sort -k1 -nr|head -n10"}' access.log统计每个URL数量和返回内容总大小:# awk '{a[$7]++;size[$7]+=$10}END{for(v ina)print a[v],v,size[v]}' access.log统计每个IP访问状态码数量:# awk '{a[$1" "$9]++}END{for(v ina)print v,a[v]}' access.log统计访问IP是404状态次数:# awk '{if($9~/404/)a[$1" "$9]++}END{for(i in a)print v,a[v]}' access.log 2、两个文件差异对比 文件内容:# seq 1 5 >

a# seq 3 7 > b找出b文件在a文件相同记录:方法1:# awk 'FNR==NR{a[$0];next}{if($0 in a)print $0}' a b345# awk 'FNR==NR{a[$0];next}{if($0 in a)print FILENAME,$0}' a bb 3b 4b 5# awk 'FNR==NR{a[$0]}NR>FNR{if($0 ina)print $0}' a b345# awk 'FNR==NR{a[$0]=1;next}(a[$0]==1)' a b # a[$0]是通过b文件每行获取值,如果是1说明有# awk 'FNR==NR{a[$0]=1;next}{if(a[$0]==1)print}' a b345方法2:# awk 'FILENAME=="a"{a[$0]}FILENAME=="b"{if($0 in a)print $0}' a b345方法3:# awk 'ARGIND==1{a[$0]=1}ARGIND==2 && a[$0]==1' a b345找出b文件在a文件不同记录:方法1:# awk 'FNR==NR{a[$0];next}!($0 in a)' a b67# awk 'FNR==NR{a[$0]=1;next}(a[$0]!=1)' a b# awk'FNR==NR{a[$0]=1;next}{if(a[$0]!=1)print}' a b67方法2:# awk'FILENAME=="a"{a[$0]=1}FILENAME=="b" && a[$0]!=1' a b方法3:# awk 'ARGIND==1{a[$0]=1}ARGIND==2 && a[$0]!=1' a b

3、合并两个文件

文件内容:# cat azhangsan 20lisi 23wangwu 29# cat bzhangsan manlisi womanwangwu man将a文件合并到b文件:方法1:# awk 'FNR==NR{a[$1]=$0;next}{print a[$1],$2}' a bzhangsan 20 manlisi 23 womanwangwu 29 man方法2:# awk 'FNR==NR{a[$1]=$0}NR>FNR{print a[$1],$2}' a bzhangsan 20 manlisi 23 womanwangwu 29 man将a文件相同IP的服务名合并:# cat a192.168.1.1: httpd192.168.1.1: tomcat192.168.1.2: httpd192.168.1.2: postfix192.168.1.3: mysqld192.168.1.4: httpd# awk 'BEGIN{FS=":";OFS=":"}{a[$1]=a[$1] $2}END{for(v in a)print v,a[v]}' a192.168.1.4: httpd192.168.1.1: httpd tomcat192.168.1.2: httpd postfix192.168.1.3: mysqld解读:数组a存储是$1=a[$1] $2,第一个a[$1]是以第一个字段为下标,值是a[$1] $2,也就是$1=a[$1] $2,值的a[$1]是用第一个字段为下标获取对应的值,但第一次数组a还没有元素,那么a[$1]是空值,此时数组存储是192.168.1.1=httpd,再遇到192.168.1.1时,a[$1]通过第一字段下标获得上次数组的httpd,把当前处理的行第二个字段放到上一次同下标的值后面,作为下标192.168.1.1的新值。此时数组存储是192.168.1.1=httpd tomcat。每次遇到相同的下标(第一个字段)就会获取上次这个下标对应的值与当前字段并作为此下标的新值。

4、将第一列合并到一行

# cat file1 2 34 5 67 8 9# awk '{for(i=1;i

< =NF;i++)a[i]=a[i]$i" "}END{for(vin a)print a[v]}' file1 4 72 5 83 6 9解读:for循环是遍历每行的字段,NF等于3,循环3次。读取第一行时:第一个字段:a[1]=a[1]1" " 值a[1]还未定义数组,下标也获取不到对应的值,所以为空,因此a[1]=1 。第二个字段:a[2]=a[2]2" " 值a[2]数组a已经定义,但没有2这个下标,也获取不到对应的值,为空,因此a[2]=2 。第三个字段:a[3]=a[3]3" " 值a[2]与上面一样,为空,a[3]=3 。读取第二行时:第一个字段:a[1]=a[1]4" " 值a[2]获取数组a的2为下标对应的值,上面已经有这个下标了,对应的值是1,因此a[1]=1 4第二个字段:a[2]=a[2]5" " 同上,a[2]=2 5第三个字段:a[3]=a[3]6" " 同上,a[2]=3 6读取第三行时处理方式同上,数组最后还是三个下标,分别是1=1 4 7,2=2 5 8,3=36 9。最后for循环输出所有下标值。 5、字符串拆分 字符串拆分:方法1:# echo "hello" |awk -F '''{for(i=1;i< =NF;i++)print $i}'hello方法2:# echo "hello" |awk '{split($0,a,"''");for(v in a)print a[v]}'lohel 6、统计出现的次数 统计字符串中每个字母出现的次数:# echo "a.b.c,c.d.e" |awk -F'[.,]' '{for(i=1;i< =NF;i++)a[$i]++}END{for(v in a)print v,a[v]}'a 1b 1c 2d 1e 1 7、费用统计 得出每个员工出差总费用及次数:# cat azhangsan 8000 1zhangsan 5000 1lisi 1000 1lisi 2000 1wangwu 1500 1zhaoliu 6000 1zhaoliu 2000 1zhaoliu 3000 1# awk '{name[$1]++;cost[$1]+=$2;number[$1]+=$3}END{for(v in name)print v,cost[v],number[v]}' azhangsan 5000 1lisi 3000 2wangwu 1500 1zhaoliu 11000 3 8、获取某列数字最大数 # cat aa b 1c d 2e f 3g h 3i j 2获取第三字段最大值:# awk 'BEGIN{max=0}{if($3>

max)max=$3}END{print max}' a3打印第三字段最大行:# awk 'BEGIN{max=0}{a[$0]=$3;if($3>max)max=$3}END{for(v in a)if(a[v]==max)print v}'ag h 3e f 3

9、去除文本第一行和最后一行

# seq 5 |awk'NR>2{print s}{s=$0}'234解读:读取第一行,NR=1,不执行print s,s=1读取第二行,NR=2,不执行print s,s=2 (大于为真)读取第三行,NR=3,执行print s,此时s是上一次p赋值内容2,s=3最后一行,执行print s,打印倒数第二行,s=最后一行

10、获取Nginx upstream块内后端IP和端口

# cat aupstream example-servers1 { server 127.0.0.1:80 weight=1 max_fails=2fail_timeout=30s;}upstream example-servers2 { server 127.0.0.1:80 weight=1 max_fails=2fail_timeout=30s; server 127.0.0.1:82 backup;}# awk '/example-servers1/,/}/{if(NR>2){print s}{s=$2}}' a127.0.0.1:80# awk '/example-servers1/,/}/{if(i>1)print s;s=$2;i++}' a# awk '/example-servers1/,/}/{if(i>1){print s}{s=$2;i++}}' a127.0.0.1:80解读:读取第一行,i初始值为0,0>1为假,不执行print s,x=example-servers1,i=1读取第二行,i=1,1>1为假,不执行prints,s=127.0.0.1:80,i=2读取第三行,i=2,2>1为真,执行prints,此时s是上一次s赋值内容127.0.0.1:80,i=3最后一行,执行print s,打印倒数第二行,s=最后一行。这种方式与上面一样,只是用i++作为计数器。以上就是AWK的10个经典案例分别是怎么样的,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report