【改變 AWK 切割欄位的方式 & 使用者定義函數】
$ vim reformat3.awk
BEGIN {
{ “date” | getline
print “Today is: “, $2, $3 > “today_rpt3”
print “====================” > “today_rpt3”
print “ID Number Arrival Time” > “today_rpt3”
close(“today_rpt3″)
}
{ FS=”[ t:]+”
sys_call = “sort -n >> today_rpt3”
}
}
{ arrival = HM_to_M($2, $3)
total += arrival
printf(“%6s %2s:%2s %sn”, $1, $2, $3, arrival > 480 ? “–” : “”) | sys_call
}
END {
close(“today_rpt3”)
close(sys_call)
printf(“Average arrival time = %d:%dn”, total/NR/60, (total/NR)%60) >> “today_rpt3”
}
function HM_to_M(hour, min) {
return hour * 60 + min
}
-> vim 命令模輸入 “:wq” (存檔離開)
$ awk -f reformat3.awk arr.txt
$ cat today_rpt3
Today is: 2月 2
====================
ID Number Arrival Time
1005 8:12 —
1006 7:45
1008 8:01 —
1012 7:46
1025 7:27
1028 7:49
1029 7:57
1034 7:26
1042 7:59
1051 7:51
1052 8:05 —
1101 7:32
Average arrival time = 7:49
【資料參考來源】