awk 在行首/行尾插入服务器时间

awk 在行首/行尾插入服务器时间

我需要一些关于 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

获取日期来计算要插入的字符串。

如果您想简单地对结果进行排序,标准免责声明“日月名年时分秒”是一个糟糕的选择。考虑年月日时分秒。

相关内容