【问题标题】:Inserting timestamps into stream in bash [duplicate]在bash中将时间戳插入流中[重复]
【发布时间】:2017-04-05 12:01:51
【问题描述】:

我有一个在标准输入上接收到的数据流,我想将时间戳添加到流中。理想情况下,我想用当前纪元秒数替换新行

... | sed "s/$/$(date +' %s')"/ 不起作用,因为它只会在命令上评估 date 命令一次。

我能做什么?

【问题讨论】:

标签: bash sed timestamp


【解决方案1】:

根据您的操作系统,您可以

  1. 使用ts 命令: 这会将时间戳放在行的开头

    ... | ts '%s'
    
  2. 使用 GNU awk

    ... | gawk '{print $0, systime()}'
    
  3. 或perl

    ... | perl -lpe '$_ .= " ".time'
    

【讨论】:

    【解决方案2】:

    我不能将此标记为重复或对其发表评论,但请查看之前在此处提出的相同问题:Is there a Unix utility to prepend timestamps to stdin?

    从那里,您可以使用ts(必须通过apt-get install moreutils 安装)或awk

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2018-07-04
      • 2017-03-06
      • 1970-01-01
      相关资源
      最近更新 更多