
このコードを持っています。パラメータを追加したい(空港名)上記のコードにしたがって、空港の名前を指定すると、その指定されたパラメータに対してのみ同じ出力が得られます。
コード:
週の各曜日における遅延便の割合、遅延便の総数、および総便数を取得します。
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 スクリプトへのパラメータとして定義されており、必要に応じて変更します。