1. 程式人生 > >awk資料處理工具用法簡介

awk資料處理工具用法簡介

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

以上。