awk作用
处理文件信息:文本文件信息 日志文件信息 配置文件信息
处理文件方式:排除信息 查询信息 统计信息 替换信息 对文件列进行处理
image.png
awk实践操作
创建环境
[root@oldboy63-libo ~]# 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
显示xiaoyu的姓氏和ID号码
[root@oldboy63-libo ~]# awk '$2~/Xiaoyu/{print $1,$3}' reg.txt
Zhang 390320151
姓氏是Zhang的人,显示他的第二次捐款金额及他的名字
[root@oldboy63-libo ~]# awk -F "[: ]+" '$1~/Zhang/{print $1,$2,$5}' reg.txt
Zhang Dandan 100
Zhang Xiaoyu 90
显示所有以41开头的ID号码的人的全名和ID号码
[root@oldboy63-libo ~]# awk '$3~ /^41/{print $1,$2,$3}' reg.txt
Zhang Dandan 41117397
Liu Bingbing 41117483
显示所有ID号码最后一位数字是1或5的人的全名
[root@oldboy63-libo ~]# awk '$3~/[15]$/{print $1,$2,$3}' reg.txt
Zhang Xiaoyu 390320151
Wu Waiwai 70271111
Wang Xiaoai 3515064655
Li Youjiu 918391635
获取文件中有井号或空行的内容, 将空行和井号信息的行排除
image.pngZhang 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
Lao Nanhai Xiaoyu :250:100:175
[root@oldboy63-libo ~]# awk '$0!~/#|^$/' 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
显示Xiaoyu的捐款,每个捐款数额都是以$开头
利用sed
[root@oldboy63-libo ~]# awk '$2~/Xiaoyu/{print $4}' reg.txt|sed 's#:#$#g'
$155$90$201
利用tr
[root@oldboy63-libo ~]# awk '$2~/Xiaoyu/{print $4}' reg.txt|tr ':' '$'
$155$90$201
awk替换
[root@oldboy63-libo ~]# awk '$2~/Xiaoyu/{gsub(/:/,"$",$NF);print $NF}' reg.txt
$155$90$201
$NF 表示文件的最后一列信息
比较行信息
root@oldboy63-libo ~]# awk 'NR>2' reg.txt
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
[root@oldboy63-libo ~]# awk 'NR<2' reg.txt
Zhang Dandan 41117397 :250:100:175
取出多行信息
连续多行
[root@oldboy63-libo ~]# awk 'NR==2,NR==4' reg.txt
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
不连续多行
[root@oldboy63-libo ~]# awk 'NR==2;NR==4' reg.txt
Zhang Xiaoyu 390320151 :155:90:201
Wu Waiwai 70271111 :250:80:75
BEGIN{动作}: 在处理文件之前,先做什么事情
添加信息
[root@oldboy63-libo ~]# awk 'BEGIN{print "姓","名","号码","捐款记录"}{print $0}' reg.txt|column -t
姓 名 号码 捐款记录
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
计算
[root@oldboy63-libo ~]# awk "BEGIN{print 2+2}"
4
修改内置变量
NF: 取出最后一列
NR: 表示行信息
awk '{print $(NF-1)}' reg.txt
END{动作}: 在处理文件之后,再做什么事情
[root@oldboy63-libo ~]# awk 'BEGIN{print "姓","名","号码","捐款记录"}{print $0}END{print "学生捐款登记表"}' reg.txt|column -t
姓 名 号码 捐款记录
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
学生捐款登记表
可以将统计后的最终结果进行输出
awk对文件进行统计分析
image.png统计行数
[root@oldboy63-libo ~]# awk '{i=i+1}END{print i}' reg.txt
8
[root@oldboy63-libo ~]# awk '{i++}END{print i}' reg.txt
8
sum=sum+n 你要对文件第几列信息做求和运算
[root@oldboy63-libo ~]# seq 10|awk '{sum=sum+$1}END{print sum}'
55
网友评论