Search Multiple Words / String Pattern Using grep Command

by Vivek Gite on April 5, 2008 · 40 comments

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

This FAQ entry is 3 of 7 in the "Linux / UNIX grep Command Tutorial" series. Keep reading the rest of the series:
Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 40 comments… read them below or add one }

1 Ayman Elkazzaz June 19, 2008

you forget “\” so the command to search multiple string using grep as following
grep ‘warning\|error\|critical’ /var/log/messages

Reply

2 Matt January 18, 2012

Thank you for this. I couldn’t figure out why it wasn’t working.

Reply

3 S. Mohana July 16, 2008

It is good explain for grep

but i want find out one particular string from another string. shall i use grep.

Reply

4 John September 17, 2008

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,

Reply

5 vivek September 17, 2008

Use the -f FILE option to obtain patterns from FILE, one per line:
grep -f words.txt /var/log/messages
words.txt

word1
word2
wordN

Reply

6 Hemal July 7, 2011

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.

Reply

7 Mats August 30, 2011

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 ?

Reply

8 pop richards August 30, 2011

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.

Reply

9 pfaure February 4, 2009

How do you search for 2 strings on the same line?, but return the following line?

Reply

10 Nauman Ali March 5, 2009

how do we search for two words and return it when both words are exists in the line. just like AND operator.

Reply

11 relesh September 23, 2009

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

Reply

12 relesh September 23, 2009

in solaris

Reply

13 Robert Guest September 25, 2009

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.

Reply

14 Dr.Death October 13, 2010

try
echo “hi rob, where is bob?” | grep -E -o “rob|bob”

it should return:
rob
bob

Reply

15 anoop July 27, 2011

use
grep ‘rob’ | grep ‘bob’

Reply

16 Robert Guest September 25, 2009

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.

Reply

17 Sarat December 11, 2009

Thanks a lot.This saved my time. We have a delivery tomorrow.

Reply

18 Sasikala January 15, 2010

@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

Reply

19 johnny January 15, 2010

Sasikala: THANK YOU!!!! That was very thoughtful of you to leave that for us, I needed it bad!!! :)

Reply

20 Nagendra Prasath January 30, 2010

grep -Rl word1 *| xargs grep -l word2… how this work?

Reply

21 karatedog February 27, 2010

“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.

Reply

22 karatedog February 27, 2010

@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.

Reply

23 GUSTAVO COST June 2, 2010

Just try this one:

grep word1 | grep word2

Reply

24 GUSTAVO COSTA June 2, 2010

correction … :
grep word1 filename | grep word2

Reply

25 Anthony June 20, 2011

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.

Reply

26 MUAnis July 13, 2010

How do you search for the string below in a file using grep ?

DeptId = ’123459′

Any help is appreciated.

Reply

27 Omprakash September 15, 2010

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

Reply

28 Ritika December 16, 2010

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

Reply

29 richardspop March 8, 2011

try
$grep -r “word1″ /Folder/subfolder/ | grep “word2″ > search.log

Reply

30 vaibhav March 20, 2011

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

Reply

31 karatedog March 20, 2011

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.

Reply

32 kumar April 25, 2011

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

Reply

33 ajorpheus May 11, 2011

@Kumar and others

To search for multiple strings in a file try doing this :

grep -il "String1" "PATH-OF-FILE" | xargs -I % grep -H "String2" % | xargs -I % grep -H "String3" %

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 :

find "/cygdrive/e/MyDocs/Downloads/work/OATS Domain related/" -iname "log4j*.xml" | xargs -I % grep -ilr "CONSOLE" "%" | xargs -I % grep -H "ASYNC" %

Cheers!

Reply

34 nithya May 17, 2011

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..

Reply

35 richardspop May 17, 2011

do u have any keyword to identify the error message??
for eg. if the keyword is ‘error:’

then

grep -rn “error:*”

Reply

36 richardspop May 17, 2011

its
grep -n “error:*” filename

since its one file -r is not necessary.

Reply

37 Saravanan August 23, 2011

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????

Reply

38 Jass September 26, 2011

/usr/xpg4/bin/grep -E ‘error|critical’ sample.txt

this works for me..

Reply

39 Abdulla October 19, 2011

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.

Reply

40 Ramon December 8, 2011

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?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 14 + 13 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: