-
三剑客命令之awk
awk
[root@oldboy63 ~]# ls -l ` which awk`
lrwxrwxrwx. 1 root root 4 Jul 13 15:25 /usr/bin/awk -> gawk
awk是gawk的软链接
gawk ---- pattern scanning and processing language
模式扫描和处理文件语言
语法:awk [参数] '模式{动作}' 文件
自己理解:'模式{动作}' 这里好像是用单引号,双引号不支持
原理图:
awk作用:
1.处理文件信息:文本文件信息 、日志文件信息、配置文件信息
2.处理文件方式:排除信息 、查询信息、统计信息、替换信息、对文件列进行处理
awk操作
创建测试环境
[root@oldboy63 ~]# 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@oldboy63 ~]# awk '$2~/Xiaoyu/{print $1,$3}' reg.txt
Zhang 390320151
[root@oldboy63 ~]# awk '$2~/Xiaoyu/{print $1" "$3}' reg.txt
Zhang 390320151
$n ---- 匹配第几列
~ ----- 匹配信息
!~ ------将匹配的信息进行取反
// -----指定匹配信息
姓氏是Zhang的人,显示他的第二次捐款金额和他的名字
[root@oldboy63 ~]# awk -F '[: ]+' '$1~/Zhang/{print $1,$2,$5}' reg.txt
Zhang Dandan 100
Zhang Xiaoyu 90
-F : 指定列分割符号(不指定符号默认是空格)
显示所有以41开头的ID号码的人的全名和ID号
[root@oldboy63 ~]# awk '$3~/^41/{print $1,$2,$3}' reg.txt
Zhang Dandan 41117397
Liu Bingbing 41117483
[root@oldboy63 ~]# awk '$3~/^41/{print $1,$2,$3}' reg.txt|column -t
Zhang Dandan 41117397
Liu Bingbing 41117483
显示所有ID号码最后一位数字是1或5的人的全名
awk '$3~/1$|5$/' reg.txt
awk '$3~/[15]$/' reg.txt
awk '$3~/(1|5)$/' reg.txt
[root@oldboy63 ~]# awk '$3~/[15]$/{print $1,$2}' reg.txt
Zhang Xiaoyu
Wu Waiwai
Wang Xiaoai
Li Youjiu
Lao Nanhai
[root@oldboy63 ~]# awk '$3~/[15]$/{print $1,$2}' reg.txt |column -t
Zhang Xiaoyu
Wu Waiwai
Wang Xiaoai
Li Youjiu
Lao Nanhai
获取文件中有井号或空行的内容,将空行和井号信息的行排除
第一个步骤: 找出需要编写模式信息
awk '/#|^$/' reg.txt
awk '$0~/#|^$/' reg.txt
说明: $0 表示将文件所有列信息进行显示
awk '$0!~/#|^$/' reg.txt
awk '!/#|^$/' reg.txt
第二个步骤: 根据条件做处理
[root@oldboyedu ~]# awk '$0!~/#|^$/' reg.txt
Zhang Dandan 41117397 :250:100:175
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
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@oldboyedu ~]# awk '$0!~/#|^$/{print $0}' reg.txt
Zhang Dandan 41117397 :250:100:175
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
Lao Nanhai Xiaoyu :250:100:175
显示Xiaoyu的捐款,每个捐款数额都是以110$220$330
[root@oldboy63 ~]# awk '$2~/Xiaoyu/{print $4}' reg.txt |sed 's#:#$#g'
$155$90$201
[root@oldboy63 ~]# awk '$2~/Xiaoyu/{print $4}' reg.txt|tr ':' '$'
$155$90$201
[root@oldboy63 ~]# awk -F "[: ]+" '$2~/Xiaoyu/{print "$"$4,"$"$5,"$"$6}' reg.txt
$155 $90 $201
awk替换
gsub = gawk substitute
gsub(/要替换的信息/,"替换成什么".$将第几列)
[root@oldboy63 ~]# awk '$2~/Xiaoyu/{gsub(/:/,"$",$NF);print $NF}' reg.txt
$155$90$201
说明:$NF 表示文件的最后一列信息
awk命令模式分类
[root@oldboy63 ~]# 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
Lao Nanhai 918391635 :250:100:175
[root@oldboy63 ~]# awk 'NR<2' reg.txt
Zhang Dandan 41117397 :250:100:175
NR ---- 表示文件行信息
[root@oldboy63 ~]# 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 ~]# awk 'NR==2;NR==4' reg.txt -----取不连续多行
Zhang Xiaoyu 390320151 :155:90:201
Wu Waiwai 70271111 :250:80:75
特殊模式:
BEGIN{动作}:在处理文件之前,先做什么事情
[root@oldboy63 ~]# 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
Lao Nanhai 918391635 :250:100:175
用于计算:
[root@oldboy63 ~]# awk "BEGIN{print 2^2}"
4
[root@oldboy63 ~]# awk "BEGIN{print 2**2}"
4
[root@oldboy63 ~]# awk "BEGIN{print 2%2}"
0
[root@oldboy63 ~]# awk "BEGIN{print 2+2}"
4
[root@oldboy63 ~]# awk "BEGIN{print 2-2}"
0
[root@oldboy63 ~]# awk "BEGIN{print 3/2}"
1.5
修改内置变量:
NF: 取出最后一列
[root@oldboy63 ~]# awk '{print $(NF-1)}' reg.txt
41117397
390320151
80042789
70271111
41117483
3515064655
1986787350
918391635
918391635
FS: 指定列分隔符, 默认识别空格为分割符
[root@oldboy63 ~]# awk 'BEGIN{FS="[: ]+"}{print $4}' reg.txt
250
155
250
250
250
50
250
175
250
[root@oldboy63 ~]# awk -vFS="[: ]+" '{print $1,$4}' reg.txt
Zhang 250
Zhang 155
Meng 250
Wu 250
Liu 250
Wang 50
Zi 250
Li 175
Lao 250
v ------ 指定变量信息
END{动作}:在处理文件之后,再做什么事情
[root@oldboy63 ~]# 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
Lao Nanhai 918391635 :250:100:175
学生捐款登记表
awk对文件进行统计分析
1.awk统计运算公式
a.文本信息累加运算
i=i+1
统计文件行数
awk '{i=i+1}END{print i}' test.txt
[root@oldboy63 ~]# cat >>test.txt<<EOF
> oldboy
> oldboy
> oldboy
> oldgirl
> Oldboy
> Oldbaby
> EOF
[root@oldboy63 ~]# awk '/oldboy/{i=i+1}END{print i}' test.txt
3
[root@oldboy63 ~]# awk '/oldboy/{i++}END{print i}' test.txt
3
b.数值信息求和运算
sum=sum+$n(要对文件第几列信息做求和运算)
[root@oldboy63 ~]# seq 10|awk '{sum=sum+$1}END{print sum}'
55
[root@oldboy63 ~]# seq 10|awk '{print sum=sum+$1}'
1
3
6
10
15
21
28
36
45
55
统计日志错误信息
awk '/Failed/{i++}END{print i}' secure-20161219
awk '$6~/Failed/{i++}END{print i}' secure-20161219'
windows文件如何传输到linux系统
1.直接拖拽
2.yum install -y lrzsz --- 实现windows和linux系统之间数据传输
rz -- 将windows数据 ---> linux系统