awk是一个好用的数据处理工具,awk命令简单用法介绍:
举例:data.txt:
stnumber name subject1 score1 subject2 score2 Total
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" " '{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
以上。