Search Multiple Words / String Pattern Using grep Command
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
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- Finding a File containing a particular text string in Linux server
- Howto use grep command in Linux / UNIX
- Bash Shell: Replace a string with another string in all files using sed and perl -pie
- How To Search Shell Command History
- UNIX / Linux: vi / vim perform search and replace operation
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
Tags: critical words, grep, grep command, log messages, regular expression, regular expression pattern, string pattern, syntax ~ Last updated on: May 5, 2008



Recent Comments
Today ~ 17 Comments
Today ~ 2 Comments
Today ~ 37 Comments
Today ~ 46 Comments
Yesterday ~ 2 Comments