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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
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!
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 05/5/08



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