How to Search Multiple Words or String Patterns Using grep Command

Table of Contents
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:
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:
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:
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:
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:
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.
LATEST POSTS
-
How to Install Anaconda on CentOS 7
-
How to Rename Local and Remote Branches in Git
-
Bash Case Statement with Examples
-
How to Install FFmpeg on Fedora 29
-
How To Secure Nginx with Let’s Encrypt SSL on Debian 9
-
How to Install Papper Flash on Ubuntu
-
How to Install PHP on Debian 10
-
How to Stop and Disable FirewallD on CentOS 7
-
How to Install Eclipse IDE on Linux Mint 19
-
Concatenate Strings in Bash Script
-
How to Install IntelliJ IDEA IDE on Linux Mint 19
-
How to Reboot Linux System with Command Line