abhinavkulkarni
abhinavkulkarni

Reputation: 2389

Search for multiple patterns in a file not necessarily on the same line

I need a unix command that would search multiple patterns (basically an AND), however those patterns need not be on the same line (otherwise I could use grep AND command). For e.g. suppose I have a file like following:

This is first line.
This is second line.
This is last line.

If I search for words 'first' and 'last', above file should be included in the result.

Upvotes: 0

Views: 448

Answers (2)

Steve
Steve

Reputation: 54392

I think instead of AND you actually mean OR:

grep 'first\|last' file.txt

Results:

This is first line.
This is last line.

If you have a large number of patterns, add them to a file; for example if patterns.txt contains:

first
last

Run:

grep -f patterns.txt file.txt

Upvotes: 0

CraigDouglas
CraigDouglas

Reputation: 101

Try this question, seems to be the same as yours with plenty of solutions: How to find patterns across multiple lines using grep?

Upvotes: 2

Related Questions