grep is a powerful utility to search strings, patterns and words in files, and text outputs of commands. It allows you to search one or more strings & patterns based on your requirement. grep command also allows you to customize your search using various options. Sometimes you may need to look for more than one string in a file or large text. In this article, we will look at how to grep multiple strings & patterns in a file or text.
How to Grep Multiple Strings, Patterns & Words
Here is the basic syntax for grep to look for multiple strings & patterns.
$ sudo grep [options] 'pattern1|pattern2|...' /path/to/file OR $ command | grep [options] 'pattern1|pattern2|...'
In the first command, we ask grep to look for multiple patterns (pattern1, pattern2, …) in a file while in second case, we pass (pipe) the output of another command to grep so that it can search it for required strings.
In both cases, grep will output all the lines that contain any of the listed patterns.
Let us look at examples of different use cases.
Also read : How to Check if mod_deflate is Enabled
How to Search Multiple Strings in a file
Let us say you want to search for multiple strings (water, sun) in a file /home/ubuntu/poem.txt. Here is the command to do it.
$ sudo grep 'water|sun' /home/ubuntu/poem.txt
If you don’t specify the full file path, grep will look for the file in your present working directory.
If you want to search 3 strings (water,sun, moon just add it to your search pattern as shown
$ sudo grep 'water|sun|moon' /home/ubuntu/poem.txt
The above grep command will look for a match for either of the specified strings only.
If you want to search for patterns that contain specified strings, use -E option. It will treat each string as a regular expression instead of a literal string, and allows you to include regex characters such as *, ?, {}, etc.
$ sudo grep -E 'water|sun|moon' /home/ubuntu/poem.txt OR $ sudo grep -e water -e sun -e moon /home/ubuntu/poem.txt
Also read : How to Search in Nano Text Editor
Search for Exact Match
If you want to search for exact match, use -w option.
$ sudo grep -w 'water|sun' /home/ubuntu/poem.txt
Ignore Case While Searching for Strings
If you want to ignore case while searching for multiple strings and patterns, use -i option.
$ sudo grep -i 'water|sun' /home/ubuntu/poem.txt
Also read : How to Create User in MongoDB
Get Count of Occurences
If you want to get a count of occurences of your search string in a file, use -c option.
$ sudo grep -c 'water|sun' /home/ubuntu/poem.txt
Also read : How to Copy File to Multiple Directories
Search for multiple patterns in multiple files
If you want to search for multiple strings in multiple files you can use wildcard characters to mention specific file type (e.g. *.txt files)
$ sudo grep 'water|sun' /home/ubuntu/*.txt
The above command will only look for specified search strings in all .txt files in /home/ubuntu directory but not in any of its sub directories. If you also want to look for multiple strings in different files in subdirectories then use -R option to do a recursive search.
$ sudo grep -R 'water|sun' /home/ubuntu/*.txt
The above command will also look for your search strings in .txt files in all subfolders in /home/ubuntu.
In this article, you have learnt how to search for multiple strings & patterns using grep command and its various options.
Also read : How to Install Joomla on Ubuntu
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.