Search is the most important factor which we often used in our daily routine. Some time we need to search multiple words or string in a file, and grep provides the functionality to look into a file and search more than one words.
With the combination of grep command and regular expression we can search multiple words. Syntax for searching such combination is as follow:
grep 'word1\|word2\|word3'
Below are the common examples of grep which we uses daily:
1. Search with words in any text file:
$ grep ‘restart\|reboot\|shutdown’ /var/log/messages
2. Grep currently running processes:
$ ps aux | grep ‘httpd\|exim\|named’
3. For exact match use the –w option:
$ grep -w ‘restart\|reboot\|shutdown’ /var/log/messages
The same can also achieved with the egrep command. Below are the examples:
$ egrep ‘restart|reboot|shutdown’ /var/log/messages
If you findany difficulty in using this command, please let us know.