我正在尝试找到一种方法来批量转换日志文件每一行的纪元时间戳。
据我了解,我应该遵循这个过程。
- 在日志中搜索纪元时间戳的所有迭代
- 将它们通过一个函数将它们转换为人类可读的格式
- 就地编辑它们回到正常的日志文件输出中
不幸的是,我似乎不太能得到正确的格式,这是日志文件的一小段:
"[1508371955] Auto-save of retention data completed successfully.",
"[1508375555] Auto-save of retention data completed successfully.",
"[1508379155] Auto-save of retention data completed successfully.",
"[1508382755] Auto-save of retention data completed successfully.",
"[1508386355] Auto-save of retention data completed successfully."
答案1
awk -F"[][]" '{cmd="date -d@" $2;cmd |getline $2; close(cmd)}1' infile
" Thu Oct 19 03:42:35 STD 2017 Auto-save of retention data completed successfully.",
" Thu Oct 19 04:42:35 STD 2017 Auto-save of retention data completed successfully.",
" Thu Oct 19 05:42:35 STD 2017 Auto-save of retention data completed successfully.",
" Thu Oct 19 06:42:35 STD 2017 Auto-save of retention data completed successfully.",
" Thu Oct 19 07:42:35 STD 2017 Auto-save of retention data completed successfully."
这里我们将 shell 命令设置date -d@$2
为名为 的变量cmd
。然后cmd |getline $2
读取awk
一行并将第二个字段值传递给cmd
我们打开它,然后我们需要关闭通过管道打开的命令,所以我们使用close(cmd)
这里。