2

I have a stream of data I'm receiving on stdin and I would like to add timestamps onto the steam. Ideally every line I would like to replace the new line with the current epoch seconds

... | sed "s/$/$(date +' %s')"/ won't work because it will just evaluate the date command once on command.

What can I do?

Jon
  • 1,785
  • 2
  • 19
  • 33
  • https://unix.stackexchange.com/questions/56189/appending-timestamp-stamp-along-with-log-file-lines – Zael Jul 17 '18 at 15:19

2 Answers2

3

depending on your OS, you could

  1. use the ts command: this will put the timestamp at the beginning of the line

    ... | ts '%s'
    
  2. use GNU awk

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

    ... | perl -lpe '$_ .= " ".time'
    
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

I can't flag this as a duplicate or comment on it, but take a look at the same question asked here before: Is there a Unix utility to prepend timestamps to stdin?

From there, you can use either ts (which has to be installed via apt-get install moreutils) or awk.

Community
  • 1
  • 1
edaemon
  • 819
  • 8
  • 7