Reputation: 2389
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
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
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