8

For a large logging file, how do I display those lines without "success" or not terminated with "ok"?

  • 7
    grep -Ev '(success|ok$)' – jordanm Mar 2 '16 at 18:48
  • @jordanm was going to suggest the same. This is an answer though and should be put in the right section. – Valentin Bajrami Mar 2 '16 at 18:59
  • @jordanm terrific, please make it an answer – Lee Mar 2 '16 at 19:19
15

To remove lines that contain either string, specifically with grep:

  • In one command, per jordanm's comment:

    grep -Ev 'success|ok$'
    

    or:

    grep -ve success -e 'ok$'
    

    or:

    grep -v 'success
    ok$'
    
  • In two commands:

    grep -v success file | grep -v 'ok$'

Example:

$ cat file
success something else
success ok
just something else

$ grep -Ev 'success|ok$'
just something else
$ grep -v success file | grep -v 'ok$'
just something else

To remove lines that contain both strings, specifically with grep:

grep -v 'success.*ok$' file

Example:

$ cat file
success something else
success ok
just something else

$ grep -v 'success.*ok$' file
success something else
just something else
  • if jordanm wants to answer instead, I'll delete this one. – Jeff Schaller Mar 2 '16 at 19:15
  • No, this is fine. – jordanm Mar 2 '16 at 19:21
  • 1
    Lee, I see you've accepted this answer, but I want to clarify - these greps will remove lines that contain either of those two strings; did you want to remove only lines that contain both "success" and end with "ok" ? – Jeff Schaller Mar 2 '16 at 19:43
  • No. either, not both. – Lee Mar 2 '16 at 19:58
  • thanks for clarifying; take a look at the most recent edit to your question to make sure it says what you meant. – Jeff Schaller Mar 2 '16 at 20:00
2

I would try awk

awk '/success/ { next ; } /ok$/ { next ; } { print ;}' file

where

  • /success/ { next ; } find word success and skip line
  • /ok$/ { next ; } find lower case ok and skip line
  • { print ;} implicit else : print line

as per suggestion

short awk (thanks to Stéphane Chazelas )

awk '!/success/ && !/ok$/'

which is basically not (success) and not (ok at end of line )

golfed awk (thank to cas )

awk '! /success|ok$/'

which reuse regexp, and negate it

  • Even if this works, that's not an answer to the question. – Quora Feans Mar 2 '16 at 18:57
  • 1
    Yes, it's an alternative way. – Lee Mar 2 '16 at 19:25
  • @Lee: it's not an alternative. It does not fulfill the condition "use grep to." – Quora Feans Mar 2 '16 at 19:31
  • 4
    awk '!/success/ && !/ok$/' would be more canonical. – Stéphane Chazelas Mar 2 '16 at 22:00
  • 1
    or even awk '! /success|ok$/' – cas Mar 3 '16 at 1:27
0

Adding to grep -Ev commands above: You can use egrep -v 'success|ok$' filename

  • 1
    egrep is deprecated in favor of grep -E – Dani_l Mar 3 '16 at 11:39
0

Since awk was mentioned, another alternative would be sed:

sed '/success/d;/ok$/d' file
sed -e '/success/d' -e '/ok$/d' file

or (Newer sed and future POSIX) thanks to Stéphane Chazelas

sed -E '/success|ok$/d' file
  • 1
    If you change sed -r to sed -E, then that will work in some BSD sed in addition to (not too old versions of) GNU sed. sed -E will be in the next major issue of the POSIX spec. – Stéphane Chazelas Mar 3 '16 at 10:06

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.