How to grep multiple strings in Linux?

The grep is a command-line utility used to search for a specific pattern in one or more files in Linux or Unix like systems.

It prints all the lines matching with the given pattern on the standard output. Sometimes it becomes more useful when piped with other commands on Linux.

In this article, we will discuss how to grep multiple strings or patterns in Linux.

For basic usage of this command, you can read the grep command in Linux.

How to grep multiple Strings on a Linux or Unix system

The pattern or sequence of character that you want to search using grep in one or more files is known as a regular expression. The grep command supports three types of the regular expression.

All of these are explained below.

Use single quotes in the pattern.

grep 'pattern' file(s)

Next is the extended regular expression which is given below.

egrep 'pattern1|pattern2' *.txt

Another one is Perl compatible syntax you can try this on older Unix shells.

grep -e pattern1 -e pattern2 *.pl

To search for multiple patterns with the basic regular expression syntax you can use the command as it is given below.

grep 'pattern1\|pattern2\|pattern3' file

You can use option E with the grep command to interpret the pattern as the extended regular expression. When using this syntax do not use the escape sequence i.e. \.

grep -E 'pattern1|pattern2|pattern3' file

How to grep multiple strings in a Single Command

The strings are the most basic patterns that we use with the grep command. In our example, we will find the warning, error, and critical words in /var/log/syslog which is a log file that stores global system activity data.

Now execute the given command to find these words –

grep 'warning\|error\|critical' /var/log/syslog

Use option -w with the above command to display only lines that contains these words.

grep 'warning\|error\|critical' /var/log/syslog

By default search is case sensitive you can make it insensitive by using the option -i we also recommend using option --color to highlight these words.

grep --color -i 'warning\|error\|critical' /var/log/syslog

You can see the output of this command in the given image. The words that you searched are highlighted in red.

You can also run the man command to check the grep command manual page for finding more options.

man grep

Few more Questions and Answers on using grep command to find Multiple Strings on Linux

What is a grep string?

Basically a grep string is the output of the grep command. Grep is a command line tool available in linux to find specific words or statements in a file or a stream in Linux or Unix like oparating systems.

How to grep a string in a file?

Use the grep command on the command line with appropriate options to find the string that you are searching on a file.

Is there a full form of grep?

Grep is the abbreviated form of Global Regular Expression Print. Grep and its ally commands used to search an input file or a stream to find a string or multiple strings.

Is grep capable to search multiple strings in a file?

YES! Grep can be used with pipe or other utility commands to get multiple strings from a file in a single command.

Conclusion

Here you have seen how to grep multiple strings in a Linux terminal. Now if you have a query then write us in the comments below.

Leave a Comment