我需要一些關於 printf 的幫助。
目前我有這個腳本
awk '{c[$1"\t"$7]++}END{for(x in c)if (c[x]>10){printf "from %s (%d)\n",x,c[x]}}' /checkip.log >> bb.log
它給了我
from 120.230.xx.xxx /index (16)
from 120.231.xx.xxx /index (16)
from 120.236.xx.xxx /index (16)
等等。
我想checkip.log
在每行的開頭/結尾列印我當前的 linux 伺服器時間(不是日期)。
例如
[07/Mar/2020:20:00:04 +0000] from 120.230.xx.xxx /index (16)
[07/Mar/2020:20:00:04 +0000] from 120.231.xx.xxx /index (16)
[07/Mar/2020:20:00:04 +0000] from 120.236.xx.xxx /index (16)
答案1
如果你有 GNU awk,那麼
awk 'BEGIN{ d=strftime("[%d/%b/%Y:%H:%M:%S %z]") }
{c[$1"\t"$7]++}
END{for(x in c)if (c[x]>10){printf "%s from %s (%d)\n",d,x,c[x]}}' /checkip.log >> bb.log
如果你沒有 GNU awk 那麼使用
awk -vd="$(date '+[%d/%b/%Y:%H:%M:%S %z]')" '{c[$1"\t"$7]++}
END{for(x in c)if (c[x]>10){printf "%s from %s (%d)\n",d,x,c[x]}}' /checkip.log >> bb.log
取得日期來計算要插入的字串。
如果您想簡單地對結果進行排序,標準免責聲明「日月名年時分秒」是一個糟糕的選擇。考慮年月日時分秒。