Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a log file with entries like the following:

Input

php notice bob
php notice winner
php notice blah winner
php notice asfsdf test
php erro)r bob                                                                                                
php error winner
php error trash winner
php error junk test
print this line

Output

php notice winner
php notice blah winner                                                                                        
php error winner
php error trash winner
print this line

What I'm trying to do is match all lines from a log file unless - it contains php followed by error or notice. If it contains php followed by error or notice, then it should only match if winner is also in the string. My regex-foo is weak. Thanks.

share|improve this question
add comment

2 Answers

Use this: grep 'php\s\+\(error\|notice\).*winner' mylogfile

share|improve this answer
add comment

There might be a way to get grep to pull that off in one string but I think personally I'd make it script to handle that in either perl or bash. In bash it would be something like

while read line
do
# If line contains "php error" or "php notice" and "winner", print it, else skip it
   if echo $line | grep "php error" >/dev/null  || echo $line | grep -q "php notice" >/dev/nulll; then
       if echo $line | grep "winner" > /dev/null; then
           echo $line
       fi
   else 
   # If line does not contain "php error" or "php notice", print it.
       echo $line
   fi
done

I don't have a system handy to test/debug this right now, so you may have to tweak it a bit.

share|improve this answer
    
Thanks. I'm still hoping to find a regex for it. I am joining to files in tail and one file needs to match the pattern "php (warning|error).+bcooper" but the other file doesn't. The issue is that you can't grep one of the files that goes into a tail. –  Brian Aug 30 '11 at 11:23
    
This will still work for that. I'm presuming you're using 'tail -f fileA -f fileB' If so, you just add your script to the end. 'tail -f fileA -f fileB | script' –  OldWolf Aug 30 '11 at 13:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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