Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for multiple strings in a file

I want to search for the occurrence of string1 OR string2 OR string3, etc. in a file, and print only those lines (to stdout or a file, either one). How can I easily do this in bash?

like image 359
topwoman Avatar asked Apr 06 '10 11:04

topwoman


2 Answers

you can also use awk

awk '/string1|string2|string3/' file

With awk, you can also easily use AND logic if needed.

awk '/string1/ && /string2/ && /string3/' file
like image 61
ghostdog74 Avatar answered Sep 20 '22 23:09

ghostdog74


grep "string1\|string2\|string3" file_to_search_in
like image 28
Chen Levy Avatar answered Sep 21 '22 23:09

Chen Levy