linux - awk strftime 返回时间

标签 linux bash shell awk

我正在使用这个漂亮的单行代码为我的日志文件的每一行添加时间戳:

tail -f log  | grep -a --line-buffered "." | awk '{ print strftime("%s\t"), $0; fflush() }'

不幸的是,它只提供完整的秒数,而我需要数据点之间的毫秒分辨率。是否有同样优雅的解决方案来获取 ms 时间戳?我不关心从纪元开始的时间,只关心行之间的差异。

谢谢!

最佳答案

你可以尝试更换及其 Time::HiRes 内置模块,例如:

tail -f infile | perl -MTime::HiRes=time -ne 'printf "%.3f\t%s", time(), $_'

它产生类似的东西:

1390014680.197  one
1390014680.197  two
1390014680.197  three
1390014680.197  four
1390014680.197  five
1390014680.197  six
1390014689.414  seven
1390014693.542  eight

关于linux - awk strftime 返回时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199415/

相关文章:

python - 在脚本中启动脚本 - 如何修复/bin/sh : 1: filename. py: not found

linux - Ubuntu 未启动至 GUI apt-get :/usr/local/lib/libstdc++. so.6:未找到版本 `GLIBCXX_3.4.15`

bash - macOS High Sierra 上的参数替换错误替换错误

linux - 用于列出终端前台进程组中所有进程的 Bash 脚本

bash - 删除文件夹和子文件夹中的所有隐藏文件

bash - 在 if 语句中传递参数

linux - 如何创建用于测试的虚拟流量?

c - Linux 串行读取抛出错误

json - bash 脚本 - grep 一个 json 变量

linux - 格式化 shell 命令输出