Piping through grep/awk prevents file write

So, this is weird... I'm running this command:

iotop -o -P -k -bt -d 5

I'd like to save the output relelvant to rsyslogd to a file, so I do this:

iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd

Nothing is written to the file! I can write the full output to the file:

iotop -o -P -k -bt -d 5 >> /var/log/rsyslogd

But I can't pipe the output through grep and then write to a file! How odd. I've also tried awk in place of grep. Any idea what might be causing this? Thanks.

My first guess would be stream buffering.

With grep, see if your implementation supports any buffering options (I believe GNU grep does).

With awk, see if it supports fflush(). If it does, call it after each print.

Regards,
Alister

If there are no command line options available with your particular awk or grep, you could use:

iotop -o -P -k -bt -d 5 |
while IFS= read -r line
do
  case $line in 
    (*rsyslogd*) printf "%s\n" "$line"
  esac
done >> /var/log/rsyslogd