user1007742
user1007742

Reputation: 571

Alternation with grep

I am trying to use grep to capture files that contain one of the two sentences.

To capture one sentence I use

grep -L "Could not place marker for right window edge" *log

For two sentences, to see if either of them exists in the file I tried

grep -L "Could not place marker for right window edge \| Could not place marker for left window edge" *log

But this is not working.

Any advice on this?

Upvotes: 0

Views: 354

Answers (3)

Jotne
Jotne

Reputation: 41460

Using awk

awk '/Could not place marker for right window edge|Could not place marker for left window edge/' *.log

Or this could be done like this

awk '/Could not place marker for (right|left) window edge/' *.log

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85865

I suspect the starting and trailing space you have introduced is causing the problem. Try:

$ egrep -L 'this is sentence|another different sentence' *log

Alternatively using fgrep as you are just looking for fixed strings and not regular expression:

$ fgrep -Le 'this is sentence' -e 'another different sentence' *log

If by sentence you actually mean line then you may also be interested in the -x argument.

-x, --line-regexp

Select only those matches that exactly match the whole line. (-x is specified by POSIX.)

You are using -L which displays the files without matches is this what you actually want or did you mean -l to display just the filenames that do match?

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185550

Try this 3 variations :

grep -l 'this is sentence\|another different sentence' *log
grep -lE 'this is sentence|another different sentence' *log
grep -lE '(this is sentence|another different sentence)' *log

If you want to find files that match, -L is not the right switch; instead use -l, from man grep :

-L, --files-without-match
Suppress normal output; instead print the name of each input file
from which no output would normally have been printed.  The scanning will
stop on the first match.

-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which
output would normally have been printed.  The scanning will stop on the first
match.  (-l is specified  by POSIX.)

Upvotes: 1

Related Questions