
我有這個代碼。我想新增一個參數(機場名稱)到所述代碼。因此,給定機場名稱,我會得到相同的輸出,但僅限於給定的參數。
代碼:
取得一週中每天的航班延誤百分比、延誤航班總數和航班總數。
BEGIN { FS = OFS = "," }
FNR > 1 {
total[$2]++; if ($6) delay[$2]++ }
END {
print "\"weekday\"", "\"percentage_delayed\"", "\"delayed\"", "\"total_flights\""
for (day in total) { print day, delay[day] / total[day] * 100, delay[day], total[day]}
}
輸入:
第 3 列和第 4 列是機場的名稱。
"DAY_OF_MONTH","DAY_OF_WEEK","ORIGIN","DEST","DEP_TIME","DEP_DEL15","CANCELLED","DIVERTED","DISTANCE"
1,Tuesday,ORD,GRB,1003,0.00,0.00,0.00,322.248
1,Tuesday,TUL,ORD,1027,0.00,0.00,0.00,1083.42
1,Tuesday,EWR,TYS,1848,0.00,0.00,0.00,1168.61
輸出:
"weekday", "percentage_delayed", "delayed", "total_flights"
Tuesday,10.7912,446,4133
Moday,10.2564,336,3276
Friday,26.6401,735,2759
如何為程式碼新增參數,使其僅顯示給定參數的輸出?
謝謝
答案1
$ awk -v origin='ORD' -v dest='GRB' '
BEGIN { FS=OFS= "," }
FNR > 1 && $3==origin && $4==dest { total[$2]++; if ($6) delay[$2]++ }
END {
print "weekday", "percentage_delayed", "delayed", "total_flights"
for (day in total)
print day, delay[day] / total[day] * 100, delay[day]+0, total[day]
}' infile
上面的-v origin='ORD'
和-v dest='GRB'
被定義為 awk 腳本的參數,根據您的需求更改它們。