How to Search Multiple Words or String Patterns Using grep Command

How to Search Multiple Words or String Patterns Using grep Command
How to Search Multiple Words or String Patterns Using grep Command

Search Multiple Words or String Patterns Using grep Command

The grep command used to find a particular string or pattern in one or multiple files. Grep stands for “Global Regular Expression Print”. Grep is the most powerful command in Linux. It can be also used to read Standard Output from another command. In this tutorial, you are going to learn How to Search Multiple Words or String Patterns Using grep Command.

Basic Syntax for Multiple Patterns using grep

The following is the basic syntax for grep command.

grep [OPTIONS] PATTERN [FILES]

So two find words or string pattern we can use grep command in the following way:

grep 'WORD_1\|WORD_2\|WORD_3\|WORD_4' PATH_TO_FILE_1 PATH_TO_FILE_2 PATH_TO_FILE_3

And for egrep command following syntax is used to find multiple words:

egrep 'WORD_1|WORD_2|WORD_3|WORD_4' PATH_TO_FILE_1 PATH_TO_FILE_2 PATH_TO_FILE_3

1. Find Multiple Words in File Using grep Command

To find foo and bar words in all configuration files inside the /etc directory you can use the following command:

grep 'foo\|bar' /etc/*.conf

The output should be:

"How

If you pass --color option with grep command you can see the output in color:

grep --color 'foo\|bar' /etc/*.conf

The output should be:

"How

By using -w option with the grep command you can match only words inside the file:

grep -w --color 'foo\|bar' /etc/*.conf

If you want to set the option ignore-case then you can use -i option with grep command:

grep -i --color 'foo\|bar' /etc/*.conf

You can get the count of the number of words matched in each file by using -c or --count option:

grep -c --color 'foo\|bar' /etc/*.conf

The output should be:

"How

To get the list of files which have matched words then you can use -l option and for vice-versa you can use -L option”

grep -l --color 'foo\|bar' /etc/*.conf
grep -L --color 'foo\|bar' /etc/*.conf

2. Find Multiple Words in File Using egrep Command

To find server and port words inside all the configuration files of /etc directory type following command inside the terminal:

egrep 'server|port' /etc/*.conf

If you pass --color option with the egrep command you can see the output in color, to do so type the following command in the terminal:

egrep --color 'server|port' /etc/*.conf

If you want to set the option ignore-case then you can use -i option with egrep command:

egrep -i --color 'server|port' /etc/*.conf

By using -w option with the egrep command you can match only words inside the file:

egrep -w --color 'server|port' /etc/*.conf

You can also use the Recursive search option to search also in subdirectories using -R option:

egrep -Rw --color 'server|port' /etc/*.conf

The output should be:

"How

You can get the count of the number of words matched in each file by using -c or --count option:

egrep -c --color 'server|port' /etc/*.conf

The output should be:

"How

Conclusion

You have successfully learned how to Search Multiple Words or String Patterns Using grep Command. If you have any queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here