Grep Multiple Strings in Linux

The grep command is one of the most powerful and frequently used commands in Linux. It allows users to search for specific patterns within files and output the matching lines. Grepping for one string is pretty simple, but sometimes you need to grep for multiple strings, and this isn’t quite…

The grep command is one of the most powerful and frequently used commands in Linux. It allows users to search for specific patterns within files and output the matching lines.

Grepping for one string is pretty simple, but sometimes you need to grep for multiple strings, and this isn’t quite as obvious. This blog post will teach you how to grep for multiple strings in a Linux operating system.

Grep for Multiple Strings in Linux
Grep for Multiple Strings in Linux

Grep Multiple Strings

When working with text data in Linux, you often need to search for multiple patterns within files or command outputs. The grep command is well-equipped to handle this task, thanks to its various options and flexibility.

Using the -e Option

The simplest way to search for multiple strings using grep is by employing the -e option. This option allows you to specify multiple patterns to search for in a single command.

Syntax:

grep -e "pattern1" -e "pattern2" filename

Example:

Imagine you have a file named data.txt with the following content:

server01
database02
network03
client04
service05
application06
firewall07

To search for lines containing either “server” or “firewall”, you would use:

grep -e "server" -e "firewall" data.txt

The output will be:

server01
firewall07

Using Extended Regular Expressions for Multiple Strings with -E

The -E option enables extended regular expressions, which offer more powerful pattern-matching capabilities. You can use the | operator to specify multiple patterns within a single expression.

Syntax:

grep -E "pattern1|pattern2" filename

Example:

Using the same data.txt file, you can search for “server” or “firewall” with:

grep -E "server|firewall" data.txt

The output will be the same as before:

server01
firewall07

Grep for Multiple Strings with Pipes

In scenarios where you need to chain multiple grep commands, you can use pipes (|) to pass the output of one grep command as input to another. This is particularly useful for more complex searches.

Example:

Suppose you want to search for lines containing “server” and then further filter those results to find lines containing “firewall”:

grep "server" data.txt | grep "firewall"

This command will first find all lines containing “server” and then search within those results for lines containing “firewall”.

Using the -f Option for Patterns from a File

The -f option allows you to specify a file containing multiple patterns to search for. Each line in the pattern file represents a separate pattern.

Syntax:

grep -f patternfile.txt filename

Example:

Create a file named patterns.txt with the following content:

server
firewall

Then, use the -f option to search for these patterns in data.txt:

grep -f patterns.txt data.txt

The output will be:

server01
firewall07

These methods provide a flexible and powerful way to search for multiple strings using the grep command.

Grep Command Common Arguments for Multiple Strings

Case Insensitive Search with -i

The -i option makes the search case insensitive, which means it will match patterns regardless of whether they are in uppercase or lowercase.

Syntax:

grep -i "pattern" filename

Example:

To search for “server” or “firewall” in a case-insensitive manner in the data.txt file:

grep -i -e "server" -e "firewall" data.txt

This will match lines containing “server”, “Server”, “SERVER”, and so on.

Inverting the Match with -v

The -v option inverts the match, meaning grep will output lines that do not match the specified patterns.

Syntax:

grep -v "pattern" filename

Example:

To find lines that do not contain “server” or “firewall”:

grep -v -e "server" -e "firewall" data.txt

The output will include all lines except those containing “server” or “firewall”.

Displaying Line Numbers with -n

The -n option displays the line numbers of the matching lines, which can be very useful for identifying the location of the matches within a file.

Syntax:

grep -n "pattern" filename

Example:

To search for “server” and “firewall” and display their line numbers:

grep -n -e "server" -e "firewall" data.txt

The output will be something like:

1:server01
7:firewall07

Counting Matches with -c

The -c option counts the number of matching lines rather than displaying the matches themselves.

Syntax:

grep -c "pattern" filename

Example:

To count the number of lines containing “server” or “firewall”:

grep -c -e "server" -e "firewall" data.txt

The output will show the count of matching lines:

2

Displaying Only Matching Strings with -o

The -o option outputs only the matched parts of the lines, rather than the entire lines.

Syntax:

grep -o "pattern" filename

Example:

To display only the strings “server” and “firewall”:

grep -o -e "server" -e "firewall" data.txt

The output will show:

server
firewall

Limiting Output with -m

The -m option limits the number of matching lines to the specified number. This can be useful when you only need to find a few matches.

Syntax:

grep -m num "pattern" filename

Example:

To limit the output to the first match of either “server” or “firewall”:

grep -m 1 -e "server" -e "firewall" data.txt

The output will be:

server01

These common arguments provide additional control and flexibility when using grep to search for multiple strings. Next, we will explore how to grep command output.

Recursively Search with Grep for Multiple Strings

For searching through files within directories and subdirectories, you can use the -r option. This is useful for searching large codebases or configuration directories.

Example:

To search for “error” and “warning” in all files within the /var/logs directory:

grep -r -e "error" -e "warning" /var/logs

Pattern File with the -f Option

If you have multiple patterns listed in a file, you can use the -f option to search for all these patterns at once.

Example:

Create a file named patterns.txt with the following content:

error
warning

Then, search for these patterns in system.log:

grep -f patterns.txt system.log

This command will output all lines in system.log that contain either “error” or “warning”.

Conclusion

The grep command is a powerful utility that can be used to search multiple strings. Whether you are searching through log files, command outputs, or multiple files within directories, grep provides efficient ways to find and filter the information you need.

Common use cases for grepping multiple strings include log analysis, system monitoring, and data extraction. For example, system administrators often need to parse log files for multiple error messages or warnings to troubleshoot issues quickly. Developers might use grep to search through codebases for specific functions or error patterns.

We encourage you to deepen your Linux skills by enrolling in our free Linux Fundamentals course at Server Academy. This course provides comprehensive training on essential Linux commands, shell scripting, and system administration, helping you become a proficient Linux administrator.

Free Course: Linux Fundamentals

This free course will teach you the fundamentals of Linux. You’ll learn about Linux distributions, t…

11 Lessons
1 Quizzes
1 Labs
1.5 Hr

Thank you for reading, and feel free to share your experiences with grep or ask questions in the comments below!

CREATE YOUR FREE ACCOUNT & GET OUR

FREE IT LABS

profile avatar

Paul Hill

Paul Hill is the founder of ServerAcademy.com and IT instructor to over 500,000 students online!