【使用 getline 來讀取資料】
$ vim reformat4.awk
awk ‘
BEGIN {
result = “today_rpt4”
sys_sort = “sort -n >> ” result
“date” | getline
print “Today is: “, $2, $3 > result
print “====================” > result
print “ID Number Arrival Time” > result
close(result)
FS = “[ t:]”
late_file = $2 “_late.dat”
while (getline < late_file > 0) cnt[$1] = $2
close(late_file)
}
{
arrival = HM_to_M($2, $3)
total += arrival
if (arrival > 480) {
mark = “–”
cnt[$1]++
} else mark = “”
message = cnt[$1] ? cnt[$1] ” times” : “”
printf(“%8s %2d:%2d %5s %sn”, $1, $2, $3, mark, message) | sys_sort
}
END {
close(result)
close(sys_sort)
printf(“Average arrival time = %d:%dn”, total/NR/60, (total/NR)%60) >> result
for ( any in cnt )
print any, cnt[any] > late_file
}
function HM_to_M(hour, min) {
return hour * 60 + min
}
‘ $*
-> vim 命令模輸入 “:wq” (存檔離開)
$ chmod +x reformat4.awk
$ ls -al reformat4.awk
-rwxr-xr-x 1 ming ming 183 2010-02-02 12:42 reformat4.awk
$ ./reformat4.awk arr.txt
$ cat today_rpt4
Today is: 2月 2
====================
ID Number Arrival Time
1005 8:12 — 3 times
1006 7:45
1008 8: 1 — 3 times
1012 7:46
1025 7:27
1028 7:49
1029 7:57
1034 7:26
1042 7:59
1051 7:51
1052 8: 5 — 3 times
1101 7:32
Average arrival time = 7:49
(ps. 遲到次數會隨執行幾次而有不同結果)
$ cat 2月_late.dat
1005 3
1012
1006
1008 3
1101
1025
1034
1042
1028
1029
1051
1052 3
(ps. 遲到次數會隨執行幾次而有不同結果)
【資料參考來源】