标签:print div 结果 $1 计数排序 ESS max and secure
#满足
awk从文件中读取一行内容到内存中--》判断是否满足条件--满足---执行对应的命令---输出到屏幕
#不满足
awk从文件中读取一行内容到内存中--》判断是否满足条件--不满足---继续读取文件里的内容直到文件最后
awk 参数 ‘模式{动作}‘ 文件
awk 参数 ‘条件(找谁){干啥}‘ 文件
~:表示包含的意思
gsub:表示替换,语法(gsbu(/目标/,"替换成什么",第几列))
#示例文件
[root@web02 files]# cat reg.txt
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
#显示Xiaoyu的姓氏和ID号码
[root@web02 files]# awk ‘/Xiaoyu/{print $1,$2}‘ reg.txt
Zhang Xiaoyu
#显示所有以41开头的ID号码的人的全名和ID号码
[root@web02 files]# awk ‘$3~/^41/{print $1,$2,$3}‘ reg.txt
Zhang Dandan 41117397
Liu Bingbing 41117483
#显示所有ID号码最后一位数字是1或5的人的全名
[root@web02 files]# awk ‘$3~/[15]$/{print $1,$2}‘ reg.txt
Zhang Xiaoyu
Wu Waiwai
Wang Xiaoai
Li Youjiu
Lao Nanhai
[root@web02 files]#
[root@web02 files]# cat reg.txt
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
#显示Xiaoyu的捐款.每个值时都有以$开头.如$520$200$135
[root@web02 files]# awk ‘gsub(/:/,"$",$4)‘ reg.txt
Zhang Dandan 41117397 $250$100$175
Zhang Xiaoyu 390320151 $155$90$201
Meng Feixue 80042789 $250$60$50
Wu Waiwai 70271111 $250$80$75
Liu Bingbing 41117483 $250$100$175
Wang Xiaoai 3515064655 $50$95$135
Zi Gege 1986787350 $250$168$200
Li Youjiu 918391635 $175$75$300
Lao Nanhai 918391635 $250$100$175
[root@web02 files]#
[root@web02 ~]# awk ‘BEGIN{print 100 * 2.2}‘
220
[root@web02 ~]# #统计/etc/services文件里面的空行数量
[root@web02 ~]# awk ‘/^$/{i++}END{print i}‘ /etc/services
16
处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题)
[root@web02 ~]# cat url.txt
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
#方法一:使用单个添加
[root@web02 ~]# awk -F ‘[/.]+‘ ‘{h[$2]++}END{print h["www"],h["mp3"]}‘ url.txt
3 1
#方法二:使用for循环
[root@web02 ~]# awk -F ‘[/.]+‘ ‘{h[$2]++}END{for(pol in h)print pol "\t" h[pol]}‘ url.txt
www 3
mp3 1
post 2
[root@web02 ~]#
[root@web02 files]# awk ‘/Failed password/{h[$(NF-3)]++}END{for(pol in h)print pol"\t" h[pol]}
‘ secure-20161219 218.65.30.126 17163
218.65.30.61 17163
125.16.71.175 4
169.46.38.74 9
183.136.238.78 30
218.2.0.16 10
91.223.133.33 2
222.186.50.206 3289
51.254.143.19 9
113.207.7.3 316
111.73.46.156 3206
123.31.34.141 39
187.115.73.70 9
182.100.67.119 17163
218.87.109.150 17163
218.87.109.151 17163
..........
[root@web02 files]# awk ‘/error: maximum/{h[$(NF-4)]++}END{for(pol in h)print pol "\t" h[pol]}
‘ secure-20161219 218.65.30.25 11440
218.65.30.61 2860
182.100.67.119 2860
218.65.30.53 5720
116.55.245.57 53
218.87.109.150 2860
218.87.109.151 2860
59.63.166.84 395
218.65.30.122 2859
182.100.67.120 2860
112.85.42.103 3009
218.65.30.123 2860
218.65.30.124 2860
218.87.109.154 3533
113.207.7.3 52
112.85.42.124 2860
112.85.42.107 2860
218.65.30.126 2860
112.85.42.99 2859
标签:print div 结果 $1 计数排序 ESS max and secure
原文地址:https://www.cnblogs.com/yjiu1990/p/10336256.html