Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Apologies for my ignorance, I'm fairly new to this.

I'm trying to read a log file which is being written to by a simple daemon. What I would like to do is filter my "live" output to certain - multiple - "types".

My types are: DEBUG|INFO|WARN|ERROR|FATAL

This is what I have so far, and it works for 1 case, I cannot get it working for multiple cases though.

tail -f log.txt | grep INFO

I've tried a couple things to try and say I want "WARN's & ERROR's" but nothing is really working for me. How would I correct this?

Thanks

share|improve this question
up vote 11 down vote accepted

try

tail -f log.txt | egrep 'WARN|ERROR'
share|improve this answer
    
Thanks, that's the ticket :) – Mike Oct 23 '09 at 15:15
1  
+1 grep -E also works. – quack quixote Oct 23 '09 at 15:23

In addition to switching to egrep/grep -E to get the alternation operator of extended regular expressions, you can you can also use multiple -e arguments to regular grep or even fgrep/grep -F

In fact, if your searches are all static strings (like the original question), you can even ‘downgrade’ all the way to fgrep/grep -F which might give a speed boost (since it always does direct string comparisons without regexps).

fgrep -e DEBUG -e INFO -e WARN -e ERROR -e FATAL

Also POSIX allows patterns to be separated by newlines.

# bash-ish to put a newlines into the string
fgrep $'DEBUG\nINFO\nWARN\nERROR\nFATAL'

# Standard syntax, but easier to break while editing(?):
fgrep "$(for f in DEBUG INFO WARN ERROR FATAL; do echo "$f"; done)"
share|improve this answer

This also works (regular grep and escape the pipe character):

tail -f log.txt | grep 'WARN\|ERROR'
share|improve this answer
    
That is a GNU extension to the syntax of basic regular expressions, it probably won't work for non-GNU greps. It is more portable to use egrep or grep -E and the non-escaped alternation marker (pipe). – Chris Johnsen Oct 23 '09 at 20:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .