我有这个代码。我想添加一个参数(机场名称)到所述代码。因此,给定机场名称,我会得到相同的输出,但仅限于给定的参数。
代码:
获取一周中每一天的航班延误百分比、延误航班总数和航班总数。
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 脚本的参数,根据您的需要更改它们。