1

I want to get only the number of ms from a ping command to a file Without piping to a file it works as intended. I'm a little bit lost on why > to a file does not write anything.

This works:

ping 192.168.1.1 |egrep --line-buffered -v 'PING|timeout' |sed -l -e 's/^.* time=\(.*\) ms$/\1/g'

This does not write anything to ping.dat

ping 192.168.1.1 |egrep --line-buffered -v 'PING|timeout' |sed -l -e 's/^.* time=\(.*\) ms$/\1/g' > ping.dat

What am I doing wrong?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
John
  • 11

2 Answers2

2

Each | is potentially surrounded by an stdio output buffer and an stdin input buffer. You need to find out which of those you need to turn off to get realtime piping.

This works for me:

ping localhost |egrep --line-buffered -v 'PING|timeout' |\
stdbuf -o0 sed -l -e 's/^.* time=\(.*\) ms$/\1/g' > file

Initially I had put stdbuf -i0 -o0 before every command and then found out which were unnecessary.

Petr Skocik
  • 28,816
0

If you're using GNU, then it appears the -l option to sed is incorrect: you probably want the -u (unbuffered) option. Also, the grep is superfluous. You should be able to get what you want from just the sed:

ping $HOST | sed -u -n '/ ms$/s/.* time=\(.*\) ms$/\1/p' >file.out

The -n suppresses normal matching output, while the s///p forces the output of matching lines.

Otheus
  • 6,138