5

I posted this to Superuser but got no takers: https://superuser.com/questions/832578/how-to-grep-a-continuous-stream-with-paging

I want to take a log file and filter out some irrelevant log entries, like ones at the INFO level. The above Stack Overflow answer got me part of the way:

$ tail -f php_error_log | grep -v INFO

The next piece I want is to have paging in this stream, such as with less. less +F works on continuous streams but I can't apply that to grep. How can I accomplish this?


Since posting that question I worked on it and discovered that less is waiting for the EOF to appear, and hangs until it receives it (source). This explains why trying to follow a pipe is not working. I hacked up a short script to do what I want, inelegantly:

#!/bin/bash
tail -f /data/tmp/test.txt | grep --line-buffered foo > /data/tmp/foo &
pid=$!
echo $pid
sleep 1
less +F /data/tmp/foo
kill $pid
rm /data/tmp/foo

I'm convinced it's possible to do this more elegantly, perhaps with a temp file that is automatically cleaned up without direct interaction.

1
  • How about piping to "less -B" ? – davidgo Oct 30 '14 at 7:15
4

How about this:

grep -V INFO php_error_log | less +F

Or, you can run multitail php_error_log, then once multitail starts, press e and follow the prompts to add a regular expression to filter on. For continuous monitoring of one or more log files over time, multitail is a good solution.

3
  • You can't pipe grep to less +F to get the desired behavior because less +F hangs waiting for the EOF before it will display any data. – phasetwenty Oct 30 '14 at 17:45
  • +1 for the part about multitail and the e command. – GaryBishop Dec 3 '14 at 13:56
  • I must've missed the recommendation for multitail originally. I just came back to this question and tried it, and it works. – phasetwenty Dec 4 '14 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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