How to search for multiple words or string patterns using grep command

Use grep command to search for multiple words or string patterns How to search for a string pattern

Search for multiple words or string patterns using grep command

Grep command used to find a specific string or pattern in one or more files. Grep stands for “Global Regular Expression Print”. Grep is the most powerful command in Linux. It can also be used to read standard output from another command. In this tutorial, you will learn how to search for multiple words or string patterns using the grep command.

Basic syntax for multiple patterns using grep

The following is the basic syntax: grep command.

grep [OPTIONS] PATTERN [FILES]

So search for two available words or string patterns 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 egrep The syntax following the command 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 a file using grep command

To find foo And bar Words in all configuration files in /etc You can use the following commands for directories:

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

The output looks like this:

How to search for multiple words or string patterns using grep command

If you pass --color Options can be used with the grep command to display the output in color.

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

The output looks like this:

How to search for multiple words or string patterns using grep command

using -w With options grep Commands that can match only words in the file:

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

When setting options ignore-case Then you can use -i Options using the grep command:

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

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

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

The output looks like this:

How to search for multiple words or string patterns using grep command

To get a list of files that matched the word, you can use -l Can be used for options and vice versa -L option”

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

2. Search for multiple words in a file using egrep command

To find server And port Words in all configuration files /etc Directory type following command in terminal:

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

If you pass --color With options egrep You can use commands to display the output in color. To do so, enter the following command in a terminal:

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

When setting options ignore-case Then you can use -i With options egrep command:

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

using -w With options egrep Commands that can match only words in the file:

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

You can also search subdirectories using the recursive search option -R option:

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

The output looks like this:

How to search for multiple words or string patterns using grep command

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

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

The output looks like this:

How to search for multiple words or string patterns using grep command

Conclusion

You have learned how to search for multiple words or string patterns using the grep command. If you have any questions about this, don’t forget to comment below.

Sidebar