模拟测试环境
此处数据说明:
第一列是用户的姓
第二列是用户的名
第三列是用户的身份id
每一行的最后以冒号分隔的三个数字,从左到右
依次表示该用户三次支付的金额。
[root@db01 scripts]# cat info.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
1)按照行号,查询某(几)行信息
##第三行数据
[root@db01 scripts]# awk 'NR==3' info.txt
Meng Feixue 80042789 :250:60:50
##第三行和第六行数据
[root@db01 scripts]# awk 'NR==3;NR==6' info.txt
Meng Feixue 80042789 :250:60:50
Wang Xiaoai 3515064655 :50:95:135
##第三行至第六行数据
[root@db01 scripts]# awk 'NR==3,NR==6' info.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
按照字符,查询某(几)行信息
[root@db01 scripts]# awk '/Meng/' info.txt
Meng Feixue 80042789 :250:60:50
[root@db01 scripts]# awk '/Wu/;/Wang/' info.txt
Wu Waiwai 70271111 :250:80:75
Wang Xiaoai 3515064655 :50:95:135
[root@db01 scripts]# awk '/Wu/,/Wang/' info.txt
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
2)找到Liu Bingbing那一行的数据,以空格为分隔符,打印第一列和第三列的数据,分别用空格和逗号隔开。
空格隔开:awk '/Liu/{print $1,$3}' info.txt
输出结果:Liu 41117483
逗号隔开:awk '/Liu/{print $1","$3}' info.txt
输出结果:Liu,41117483
3)打印出Meng Feixue第二次支付的金额数目
awk -F ":" '/Meng/{print $(NF-1)}' info.txt
此处-F参数表示以什么作为分隔符
$NF表示最后一列
$(NF-1)表示倒数第二列
4)打印出第三次支付金额数字的个位数是0或1的人名
##方法一
[root@db01 scripts]# awk '/[10]$/{print $1,$2}' info.txt
Zhang Xiaoyu
Meng Feixue
Zi Gege
Li Youjiu
##方法二
[root@db01 scripts]# awk '/(1|0)$/{print $1,$2}' info.txt
Zhang Xiaoyu
Meng Feixue
Zi Gege
Li Youjiu
总结:awk命令中$符号用法
$1 $2 $3 : 取第几列信息
$NF : 取最后一列
$(NF-n) : 取倒数第(n+1)列
$0 : 取所有列的信息