I have a file with a bunch of log lines.

I want to return files that contain the word 'fail' in it, but that don't contain the pattern:

Failed any test here _fr in Pro-any text here

So if it contains: _fr in Pro-

Then ignore the line even if it has the word fail in it.


Let's consider this test file:

$ cat >test.log
keep fail
omit fail  _fr in Pro-

To find all the lines that match your criteria:

$ awk '/[fF]ail/ && ! /_fr in Pro-/' test.log
keep fail

To print just the names of files that have lines matching your criteria:

$ awk '/[fF]ail/ && ! /_fr in Pro-/ {print FILENAME; exit}' *.log

How it works:

Awk implicitly reads through a file line-by-line. The condition /[fF]ail/ && ! /_fr in Pro-/ matches any line that contains Fail or fail but does not contain _fr in Pro-. In awk && means logical-and and ! means logical-not.

Rejecting both _fr in Pro- and _en in Pro-

Consider this test file:

$ cat test.log
keep fail
omit fail  _fr in Pro-
omit fail  _en in Pro-

To reject both forms:

$ awk '/[fF]ail/ && ! /_(fr|en) in Pro-/' test.log
keep fail
  • How could _fr or _en? I tried _[fr|en] but that didn't seem to work. – cool breeze Sep 19 '16 at 17:28
  • 2
    @coolbreeze [fr|en] matches any of the characters f, r, |, e, or n. The form for logical-or is (fr|en). See updated answer for more. – John1024 Sep 19 '16 at 17:34

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.