Wednesday, August 26, 2009

Search Multiple Words / String Pattern Using grep Command

SkyHi @ Wednesday, August 26, 2009
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