【发布时间】:2016-08-30 05:00:22
【问题描述】:
假设我想为ping google.com 之类的命令添加时间戳,以便输出看起来像
[9:48:56]PING google.com (116.28.94.14): 56 data bytes
[9:48:55]64 bytes from 116.28.94.14: icmp_seq=0 ttl=54 time=13.118 ms
[9:48:57]64 bytes from 116.28.94.14: icmp_seq=1 ttl=54 time=13.943 ms
[9:48:58]64 bytes from 116.28.94.14: icmp_seq=2 ttl=54 time=19.103 ms
[9:48:59]64 bytes from 116.28.94.14: icmp_seq=3 ttl=54 time=12.854 ms
我已经研究过使用管道和命令(如 xarg 和 awk)的奇特方式,但我就是无法做到这一点。另外,假设我的环境不允许我使用像stdbuff 或unbuff 这样的花哨的东西。如果可能的话,用最类似unix的方法来实现这一点会很棒。谢谢!
【问题讨论】:
-
man ping说有一个选项-D.. 检查它有帮助 -
你没有使用管道和
awk的“奇葩方式”是什么?我通常对perl -ne 'printf "[%s]: %s", scalar(localtime), $_'很满意
标签: unix command-line stdout unix-timestamp