day19 -三剑客awk(下)

grep sed awk 过滤相同内容

grep
[root@yuyingqian ~]# grep 'oldb.y' oldboy.txt 
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my god ,i am not oldbey,but OLDBOY! 
sed
[root@yuyingqian ~]# sed -n '/oldb.y/p' oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my god ,i am not oldbey,but OLDBOY! 
awk
[root@yuyingqian ~]# awk '/oldb.y/' oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my god ,i am not oldbey,but OLDBOY!
awk按列过滤
[root@yuyingqian ~]#  cat /server/files/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

过滤出第二列以X开头的行

[root@yuyingqian ~]# awk '$2~/^X/' /server/files/reg.txt
Zhang Xiaoyu    390320151  :155:90:201
Wang  Xiaoai    3515064655 :50:95:135

过滤出第三列以5结尾的行

[root@yuyingqian ~]# awk '$3~/5$/' /server/files/reg.txt
Wang  Xiaoai    3515064655 :50:95:135
Li    Youjiu    918391635  :175:75:300
Lao   Nanhai    918391635  :250:100:175

过滤出第三列以5结尾的行 显示第三列跟第四列

[root@yuyingqian ~]# awk '$3~/5$/{print $3,$4}' /server/files/reg.txt
3515064655 :50:95:135
918391635 :175:75:300
918391635 :250:100:175

姓氏是Zhang的人,显示他的第二次捐款金额及她的名字

[root@yuyingqian ~]# awk -F"[ :]+" '$1~/Zhang/{print $1,$2,$5}' /server/files/reg.txt
Zhang Dandan 100
Zhang Xiaoyu 90

显示所有ID号码最后一位数字是1或5的人的全名

[root@yuyingqian ~]# awk '$3~/[15]$/' /server/files/reg.txt
Zhang Xiaoyu    390320151  :155:90:201
Wu    Waiwai    70271111   :250:80:75
Wang  Xiaoai    3515064655 :50:95:135
Li    Youjiu    918391635  :175:75:300
Lao   Nanhai    918391635  :250:100:175

显示所有人的全名,以姓,名的格式显示,如Meng,Feixue

[root@yuyingqian ~]# awk '{print $1","$2}' /server/files/reg.txt
Zhang,Dandan
Zhang,Xiaoyu
Meng,Feixue
Wu,Waiwai
Liu,Bingbing
Wang,Xiaoai
Zi,Gege
Li,Youjiu
Lao,Nanhai

显示Xiaoyu的捐款.每个值时都有以$开头.如$520$200$135

[root@yuyingqian ~]# awk '{gsub(/:/,"$");print $4}' /server/files/reg.txt
$250$100$175
$155$90$201
$250$60$50
$250$80$75
$250$100$175
$50$95$135
$250$168$200
$175$75$300
$250$100$175

换/oldboy/passwd.txt文件的第一列和最后一列(不用修改文件内容)

[root@yuyingqian ~]# cat /oldboy/passwd.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@yuyingqian ~]# sed -r 's#(.*)(:x.*:)(.*)#\3\2\1#g' /oldboy/passwd.txt
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin

awk调换

[root@yuyingqian ~]# awk -F: '{tmp=$1;$1=$NF;$NF=tmp;print $0 }' /oldboy/passwd.txt
/bin/bash x 0 0 root /root root
/sbin/nologin x 1 1 bin /bin bin
day19 -三剑客awk(下)_第1张图片
awk调换
awk显示时以什么分割 OFS (awk的内置变量) -v修改
[root@yuyingqian ~]# awk -F:  -vOFS=":" '{tmp=$1;$1=$NF;$NF=tmp;print $0 }' /oldboy/passwd.txt
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
BEGIN{}里面的内容会在awk读取文件之前运行
[root@yuyingqian ~]# awk -F: 'BEGIN{print "name"}{print $1}' /oldboy/passwd.txt 
name
root
bin

运行测试

[root@yuyingqian ~]# awk 'BEGIN{print 1/5,1*5,1+5,1-5}'
0.2 5 6 -4

统计文件中空行的数量

[root@yuyingqian ~]# cat /oldboy/oldboy.txt
oldboy 


dsadsa
d
sadsa sa 

dsad


dsadasd
[root@yuyingqian ~]# awk '/^$/{i=i+1;print i}' /oldboy/oldboy.txt 
1
2
3
4
5
awk计算公式:i=i+1/i++ (i从0开始)

进行计数 计算一共出现了多少次

END{}里面的内容会在awk读取文件之后运行
[root@yuyingqian ~]# awk '/^$/{i=i+1;print i}' /oldboy/oldboy.txt 
1
2
3
4
5
[root@yuyingqian ~]# awk '/^$/{i=i+1}END{print i}' /oldboy/oldboy.txt 
5

统计access.log中 第九列是200的数量

[root@yuyingqian ~]# awk '$9==200{i++}END{print i}' access.log
142666
[root@yuyingqian ~]# awk '$9~/^200$/{i++}END{print i}' access.log
142666

i+i+$1数字(求和/累加) i从0开始 $依次+1

[root@yuyingqian ~]# seq 100 |awk '{i=i+$1}END{print i}'
5050

你可能感兴趣的:(day19 -三剑客awk(下))