grep string where next line does not contain string
You can use p
erl c
ompatible r
egular e
xpressions grep
:
$ pcregrep -M '(searchString.*\n)(?!.*excludeString)' file
foo2 searchString bar
foo3 searchString bar
foo4 searchString bar
It searches searchString
followed by any char .
, repeated zero or more times *
, followed by new line \n
only if there is not (?!
) pattern .*excludeString
next to it. Option -M
is present in order to match multi lines.
With sed
:
sed '/searchString/!d;$!N;/\n.*excludeString/!P;D' infile
How it works:
/searchString/!d
deletes the line if it doesn't matchsearchString
and reads in a new line, starting the command cycle over again (i.e. the remaining commands are no longer executed)- if the line matches
searchString
,sed
executes$!N;/\n.*excludeString/!P;D
- see HERE how it works; the difference is that here, it is looking for the patternexcludeString
after the\n
ewline character so that a line matching bothsearchString
andexcludeString
is still printed if it's not followed by a line matchingexcludeString
; if there was no line matching bothsearchString
andexcludeString
(i.e. known input) then you could drop the\n.*
part and run:
sed '/searchString/!d;$!N;/excludeString/!P;D' infile