Home » Linux » Shell » Grep Multiple Words Strings Patterns

How to Grep for Multiple Words, Strings and Patterns

This guide explains how to use the grep utility for searching multiple words, strings, and patterns. You can find full examples of the commands used to accomplish this below.

What Is Grep?

When we refer to grep, we’re talking about the command-line function. Grep stands for Global Regular Expression Print. In essence, it will search input files looking for lines that are a match to a certain regular expression. It then returns results by writing each line for standard output.

The grep utility is suitable for three expression syntaxes. These can be specified as Basic, Extended or Perl-compatible. If no regular expression syntax is specified, the GNY grep will set the search patterns as basic expressions.

Grep for Multiple Patterns

If you wish to search for multiple patterns, you can use the OR operator, referred to as the alternation operator. This operator/pipe looks like this: |

It will enable the user to specify all potential matches, whether they are expression sets or literal strings. As such, in order to search for multiple patterns, the input would be:

$ grep 'first pattern\|second pattern' filename

The ‘first pattern‘ and ‘second pattern‘ would be replaced by the potential patterns you’re searching for. This regular expression – ‘first pattern\|second pattern‘ – should always be enclosed in single quotes. The pipe symbol isn’t supported as an alternation operator and as such the escape (backslash) character is required to tell grep to instruct pipe differently.

In order to specify the intended pattern as an extended regular expression, the ‘-e‘ or ‘—extended-regexp‘ can be used. For example:

$ grep -E 'first pattern|second pattern' filename

In an extended regex, you are not required to escape the pipe.

You can search by file, so searching patterns within presentation.txt might look like this:

$ grep -E 'first pattern|second pattern' presentation.txt

The output will show the strings you wish to grep highlighted.

If the file you’re searching for is within another directory, you’ll need to instruct grep to search in that specific directory by using the full file path. For example:

$ grep -E 'first pattern|second pattern' /home/presentation.txt

Grep For Multiple Strings

The most basic of patterns are literal strings. To search multiple strings, you’d enter these after the grep command. Imagine we’re searching for ‘one‘, ‘two‘, and ‘three‘. It might look like this:

$ grep 'one\|two\|three'

Follow that command with the file in which you’re searching. If you’re searching using the extended regular expression, you’re essentially removing the ‘\‘. For example:

$ grep -E 'one|two|three'

Ignore Case Sensitivity

The command ‘grep’ is case sensitive. To ignore the letter case, invoke with the ‘-i‘ or ‘—ignore-case‘ option. For instance:

$ grep -i 'one\|two\|three'

Displaying A Count Of Multiple Matches

If you’re within a log file and want to know whether the number of warning and error messages happens to increase, but without seeing fully detailed results, you can use ‘-c‘. To count the multiple matches within the myapp.log file, it might look like this:

grep -c 'one\|two\|three' /var/log/myapp.log

Grep Multiple Patterns In Specific File Types

Multiple strings can be searched in a certain file type. This allows you to search, for example, all text files or all logfiles within a directory. Using an asterisk with the file extension rather than the file name will instruct the grep. Using the above example for error messages, looking through all log files might look like this:

grep -c 'one\|two\|three' /var/log/*.log

Searching Recursively For Multiple Matches

Using grep will only search within the current directory with the asterisk in use. If you need to include all subdirectories, the ‘-R’ option can be employed:

grep -R 'one\|two\|three' /var/log/*.log

This will then return results for all files found within the specified var/log/ directory and subdirectories.

Using grep For Exact Matches

The ‘grep’ command will display all results where the string is also embedded and not just the exact match. For instance, when searching for ‘value’ it will print lines that include ‘values’ and ‘valued’ also. If you wish to return lines with only exact matches of the whole word, you can use ‘-w‘ or ‘—word-regexp‘. This might look as such if you’re searching for exact matches of ‘one‘, ‘two‘, and ‘three‘:

$ grep -w 'one\|two\|three'

Word characters are inclusive of alphanumeric, so 0-9 and a-z plus underscore characters. Any others are deemed to be non-word characters.

SHARE:
Photo of author
Author
My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Comment