[awk 練習] 使用 getline 來讀取資料

【使用 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” (存檔離開)

(閱讀全文…)

[awk 練習] AWK 中如何利用系統資源

【AWK 中如何利用系統資源】
$ vim reformat2.awk
BEGIN {
“date” | getline
printf(“Today is %s %d 日n”, $2, $3) > “today_rpt2”
print “=========================” > “today_rpt2”
print “ID Number   Arrival Time” > “today_rpt2”
close(“today_rpt2”)
}
{ printf(“%6s %10sn”, $1, $2) | “sort -n >> today_rpt2” }
-> vim 命令模輸入 “:wq” (存檔離開)

(閱讀全文…)