1

This question already has an answer here:

I basically want to write my ping statistics into a file. So far, I accomplished this with ping adress | awk '{ print strftime("[%d.%m.%Y %H:%M:%S]"), $0}' > textfile but it would be more handy to be able to look at the output without having to interrupt the process before. I am also aware that this command does not write stderr into my textfile. I came up with that part while writing this question.

The command or script should basically behave like this:

$ command > textfile [15.08.2017 00:17:07] PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. [15.08.2017 00:17:07] 64 bytes from 8.8.8.8: icmp_seq=1 ttl=44 time=11.5 ms [15.08.2017 00:17:08] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=44 time=11.5 ms ^C $ cat textfile [15.08.2017 00:17:07] PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. [15.08.2017 00:17:07] 64 bytes from 8.8.8.8: icmp_seq=1 ttl=44 time=11.5 ms [15.08.2017 00:17:08] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=44 time=11.5 ms

marked as duplicate by DopeGhoti, Sparhawk, Rui F Ribeiro, Scott, Jeff Schaller Aug 15 '17 at 1:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I don't understand what you mean. In general it is expected here to offer an example of the desired result. – Hauke Laging Aug 14 '17 at 22:11
3

Change your command to this:

ping address 2>&1 | awk '{ print strftime("[%d.%m.%Y %H:%M:%S]"), $0}' | tee textfile

2>&1 redirects stderr to stdout which goes to awk. tee allows you to have both the output on your screen and in the file you specified.

You may be annoyed by awk buffering its output which will be displayed by blocks. Then use either:

awk '{ print strftime("[%d.%m.%Y %H:%M:%S]"), $0; fflush()}'

or:

stdbuf -oL awk '{ print strftime("[%d.%m.%Y %H:%M:%S]"), $0}'
  • Strangely, the command does not output anything while both ping address 2>&1 | awk '{ print strftime("[%d.%m.%Y %H:%M:%S]"), $0}' and ping adress 2>&1 | tee textfile work just fine. I may also add that I am using zsh – Boris Aug 14 '17 at 22:36
  • awk's output may be buffered. Try one of the two alternatives I gave at the end of my answer. – xhienne Aug 14 '17 at 22:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.