awk数据处理工具用法简介

awk是一个好用的数据处理工具,awk命令简单用法介绍:
举例:data.txt:

stnumber name subject1 score1 subject2 score2 
001 liming math 77.5 English 88
002 lily math 66.5 English 66
003 rizzy math 55.5 English 99.5
004 david math 44.0 English 70
005 rouse math 98 English 100
006 nacy math 87 English 76.5
007 yumy math 92 English 72.5

显示第2,4,6列:

cat data.txt | awk -F" " 'NR>=2{print $2 "\t" $4 "\t" $6}'
liming	77.5	88
lily	66.5	66
rizzy	55.5	99.5
david	44.0	70
rouse	98	100
nacy	87	76.5
yumy	92	72.5

显示第2,4,6列,并显示行号:

awk -F" " '$4 > 60 {print $2 "\t" $4 "\t" $6 "\t lines:" NR }'
liming	77.5	88	 lines:1
lily	66.5	66	 lines:2
rouse	98	100	 lines:5
nacy	87	76.5	 lines:6
yumy	92	72.5	 lines:7

显示满足条件的行,并显示行号:

cat data.txt | awk -F" " '$6 == 100 {print $2 "\t" $4 "\t" $6 "\t lines:" NR }'
rouse	98	100	 lines:5
cat data.txt | awk -F" " '$4 > 60 {print $2 "\t" $4 "\t" $6 "\t lines:" NR }'
liming	77.5	88	 lines:1
lily	66.5	66	 lines:2
rouse	98	100	 lines:5
nacy	87	76.5	 lines:6
yumy	92	72.5	 lines:7

想要计算某几列之和,且第一行与后几行分开并格式化输出:

$ cat data.txt |  |
>awk 'NR==1{printf "%10s %10s %10s %10s %10s %10s %10s\n",$1,$2,$3,$4,$5,$6,"Total" }
>NR>=2{total = $4 + $6
>printf "%10s %10s %10s %10.2f %10s %10.2f %10.2f\n",$1,$2,$3,$4,$5,$6,total}'

结果:

  stnumber       name   subject1     score1   subject2     score2      Total
       001     liming       math      77.50    English      88.00     165.50
       002       lily       math      66.50    English      66.00     132.50
       003      rizzy       math      55.50    English      99.50     155.00
       004      david       math      44.00    English      70.00     114.00
       005      rouse       math      98.00    English     100.00     198.00
       006       nacy       math      87.00    English      76.50     163.50
       007       yumy       math      92.00    English      72.50     164.50

以上。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值