How to use the grep command to search multiple words in Linux

  

The grep command in Linux system is mainly used to search for strings and files. In fact, the grep command can search multiple words at the same time. Here's a look at how to use the grep command to search for multiple words under Linux.

Here's how:

grep ‘word1\\| Word2\\| Word3’ /path/to/file

In the following example, to find the words warning, error, and critical in a text log file called /var/log/messages, type:

$ grep ‘warning\\| Error\\| Critical’ /var/log/messages

Just match the word (that is, the word is delimited on both sides of the word, for Western-spaced languages), you can add the -w option parameter:

$ grep -w ‘warning\\| Error\\| Critical’ /var/log/messages

The egrep command can skip the above syntax format, using the following syntax:

$ egrep -w ‘warning| Error| Critical’ /var/log/messages

Add the -i (ignoring case) and --color option parameters as follows:

$ egrep -wi --color ‘ Warning| Error| Critical’ /var/log/messages

Output example:

The above picture shows the actual effect of using the grep command to search for multiple words under Linux. Now you know the grep command for Linux system. There is such an effect.

Copyright © Windows knowledge All Rights Reserved