Search Multiple Words / String Pattern Using grep Command

by Vivek Gite · 16 comments

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

A. grep command supports regular expression pattern.

Grep command example

To search multiple words, use following syntax:
grep 'word1|word2|word3' /path/to/file

For 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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 16 comments… read them below or add one }

1 Ayman Elkazzaz 06.19.08 at 11:20 pm

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

2 S. Mohana 07.16.08 at 4:39 am

It is good explain for grep

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

3 John 09.17.08 at 2:37 pm

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,

4 vivek 09.17.08 at 4:24 pm

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
5 pfaure 02.04.09 at 2:38 am

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

6 Nauman Ali 03.05.09 at 10:19 am

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

7 relesh 09.23.09 at 8:41 am

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

8 relesh 09.23.09 at 8:42 am

in solaris

9 Robert Guest 09.25.09 at 5:39 pm

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.

10 Robert Guest 09.25.09 at 5:50 pm

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.

11 Sarat 12.11.09 at 1:56 pm

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

12 Sasikala 01.15.10 at 7:18 am

@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

13 johnny 01.15.10 at 6:03 pm

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

14 Nagendra Prasath 01.30.10 at 1:03 pm

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

15 karatedog 02.27.10 at 1:50 am

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

16 karatedog 02.27.10 at 1:58 am

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All