How do I search multiple string using grep command? For example I'd like to search word1, word2, word3 and so on within /path/to/file. How do I force grep to search multiple words?
The grep command supports regular expression pattern. To search multiple words, use following syntax:
grep 'word1|word2|word3' /path/to/file
In this example, search warning, error, and critical words in a text log file called /var/log/messages, enter:
$ grep 'warning|error|critical' /var/log/messages
To just match words, add -w swith:
$ grep -w 'warning|error|critical' /var/log/messages
Facebook it - Tweet it - Print it -


{ 40 comments… read them below or add one }
you forget “\” so the command to search multiple string using grep as following
grep ‘warning\|error\|critical’ /var/log/messages
Thank you for this. I couldn’t figure out why it wasn’t working.
It is good explain for grep
but i want find out one particular string from another string. shall i use grep.
Thanks for the article. Is there a way to supply the words in a file? So in your example I could create a simple file containing:
warning
error
critical
I have over 200 words that I need to search – which is tiresome to put onto the command line.
Thanks,
Use the -f FILE option to obtain patterns from FILE, one per line:
grep -f words.txt /var/log/messageswords.txt
Fantastic !! I had a scenario where in I had to pick up lines from a log file based on a word. Unfortunately I had 1000 such words and so it would have been difficult to find 1000 lines. But this solution really helped. Thanks.
Hi,
I have a requirement in which I would need to grep/find a line based on matching 3 different patterns. I know we can grep with -E multiple parameters seprated by pipe but this work Pipe (|) as OR condition. My requirement is I want to use AND condition. It should show line where it satisfy both the parameter.
e.g. line in a file as –
10-Aug-2010 Hello, this is a new example for unix.
I need to show this line only when my grep command matches all 3 words
this, new, unix How to right such grep command ?
grep -n “$search1″ . | grep -n “$search2″ | grep -n “$search3″
after the first grep only statements “search1″ will come. this is filtered by the next grep creating and AND condition.
this is a round about solution coming out the top of my mind. if i find a better solution i’ll post it.
How do you search for 2 strings on the same line?, but return the following line?
how do we search for two words and return it when both words are exists in the line. just like AND operator.
these all are not working
try
grep -E -e ‘warning|critical|error’ /var/log/messages in Linux
/usr/xpg4/bin/grep -E -e ‘warning|critical|error’ /var/adm/messages
in solaris
How do I only return lines that have both words in the line
grep -E “rob|bob” returns lines even if only one string is present.
try
echo “hi rob, where is bob?” | grep -E -o “rob|bob”
it should return:
rob
bob
use
grep ‘rob’ | grep ‘bob’
I figured I should be more specific in my request.
I have the following line;
/usr/bin/dsmc archive -des=”DAILY” -archm=FS_DBARCH_DAILY “/brlog1/BIDW/redo/*”
I want a grep command to return the line number of this line
grep archive filename | grep -n redo doesn’t work because it returns the number of the line in the results from the first grep.
Thanks a lot.This saved my time. We have a delivery tomorrow.
@Nauman Ali,
To search for two words, and to return only if both words exist in a file, use this command
grep -Rl word1 *| xargs grep -l word2
Sasikala: THANK YOU!!!! That was very thoughtful of you to leave that for us, I needed it bad!!! :)
grep -Rl word1 *| xargs grep -l word2… how this work?
“grep -Rl word1 *| xargs grep -l word2… how this work?”
It just doesn’t work.
It will return the filename where exists at least one line where the two words are present. Which is no information at all.
@Robert Guest:
Attach the ‘-n’ parameter to the first grep, not to the second:
grep -n archive filename | grep redo
that way the second grep will get a line that contains the line number.
Just try this one:
grep word1 | grep word2
correction … :
grep word1 filename | grep word2
That won’t work if you need to check if word 1 and word2 are anywhere in the file, because the first grep returns the matching line, so the second grep would only match if both words were on the same line.
How do you search for the string below in a file using grep ?
DeptId = ’123459′
Any help is appreciated.
Hi,
I want to search multiple string using grep and want to display only matching string.
can any one pl guide me in this regards.
eg.
cat abc.txt |grep -e ‘ab’ -e ‘bc’ -e ‘cd’
If ‘bc’ is there in the file abc.txt then output should display only ‘bc’ rather than displaying the entire line.
Thanks & Regards,
Omprakash
Google for: grep multiple string and returning matching string
How to search multiple words in separate lines, inside a directory including sub-directory? Pls. give easy example.
I tried $grep -r “word1″ |grep -r “word2″ /Folder/subfolder/ > search.log
try
$grep -r “word1″ /Folder/subfolder/ | grep “word2″ > search.log
hi
I have to grep exact line in file
for eg : file name test.txt includes
ram
5) 1,2,3
sohan
5) 6,7,8
so i want to grep ram and 5 so the output shud be
ram
1,2,3
should not be
ram
1,2,3
6,7,8
First: grep is a single line utility. And because “5)” is on two lines, grep will find them, because it walks down the lines, and matches them to your rules, period.
Second: what grep finds, that ‘entire’ line will be displayed. So it is not possible to cut the “5)” from the beginning of the line, and display the rest.
If you want to find “ram” and after that the next “5)”, you need to use some utility that allows you to implement some logic. Like awk.
However if you can ensure that “5)…” will be after the found line – like “ram” – then you can use the ‘after context’ feature of grep, whic displayes the matching lines PLUS some line after that.
Hi,
I need to list the files which contains the 3 strings
<Tax
<Source
HEAD
These all 3 strings may be in different lines.
Thanks, Kumar
@Kumar and others
To search for multiple strings in a file try doing this :
For eg:
Let’s say I want to search for all those log4j.xml files which have the words CONSOLE and ASYNC in them .. then this is what I would do :
Cheers!
hi.
how to search a single line using grep command..
for example a file having 100 lines in that 100 line only one error line is there.
how do i retrive that single line using grep command.. i don’t know in which line in that error msg and like that error msg many of the lines in that file.. how do i find using ‘Grep’ command..
do u have any keyword to identify the error message??
for eg. if the keyword is ‘error:’
then
grep -rn “error:*”
its
grep -n “error:*” filename
since its one file -r is not necessary.
hi, i want to grep the lines which has Eg:”uat” string in the but not “#” string in the line.. can anyone help me out in this????
/usr/xpg4/bin/grep -E ‘error|critical’ sample.txt
this works for me..
Could you please help me regarding while connect putty Linux based logs like collect grep is working
tail -f test_log | grep ‘\” 50[0234]‘ – working
tail -f test_log | grep ‘\” 50[0234]‘ | grep “404″ – not working
Any one help me on this regard? how to collect 1.500 to 504 2.404 alone.
Great info site.
I have a need to search a file looking for dates and a string, For example:
(Dec 2 13:25:27 name local5:warn|warning vmdaemon[180412]: #415 Moved volume tape_1 #0055 (12345678) (abc123 from online to offline.). I need to search for a date and the “online to offline” string together. Also, with grep is it possible to do a date range in the search as oppose to a single date?