How to Grep for Multiple Strings, Patterns, or Words Like a Pro.

Want to search for more than one word using grep like a Linux ninja? Learn how to grep multiple strings or patterns in one go — whether it’s plain text, regex magic, or case-insensitive matching. Perfect for sysadmins, devs, and curious tinkerers alike!

How to Grep for Multiple Strings, Patterns, or Words Like a Pro.
Photo by yannick Coffi see more @ yannickcoffi -/- PRINTS -/- yC


When you're digging through logs, code, or config files, the last thing you want is to run separate grep commands for each keyword. Wouldn't it be better to search for multiple strings all at once? That's where knowing how to grep like a pro comes into play.

This guide will show you how to use grep to search for:

  • Multiple words or phrases
  • Patterns using regular expressions (regex)
  • Case-insensitive matches
  • Clean output for better readability

Grep Multiple Patterns – The Basics

The syntax is straightforward:

$ grep 'pattern1\|pattern2' SNUB_file.log

Or, if you're working with extended regular expressions:

$ grep -E 'pattern1|pattern2' SNUB_file.log

Here, the pipe | acts as an (OR) operator, telling grep to match either pattern.

What's the Deal with grepgrep -E, and egrep ?

Let's break it down:

  • grep: Global Regular Expression Print The standard and most basic form of grep. It uses Basic Regular Expressions (BRE), offering minimal support for metacharacters. Many special characters require escaping, which can make complex patterns harder to read and maintain. Ideal for simple pattern matching tasks.
  • grep -E or Egrep: Extended grep — A more advanced and expressive version of grep that enables Extended Regular Expressions (ERE). It natively supports a richer set of metacharacters like (), {}, +, ?, ^, $, or | without requiring backslashes. This makes it more efficient and user-friendly for complex pattern matching, especially in scripting and automation.
  • egrep: Old-school alias for grep -E. Functionally identical to grep -E, but considered outdated. It's still supported on many systems for backward compatibility, but modern best practices recommend using grep -E for clarity and consistency.

Stick to grep -E for readability and future-proof scripts!

How to Grep Multiple Patterns in a File

Let's say you want to find lines containing error or fail:

$ grep -E 'error|fail' SNUB_log.txt

This will return all lines that include either of those words.
 | (OR) operator in the command means either one pattern or the other, not both at the same time.

How to Search for Lines Containing Both ‘Error’ and ‘Fail’ in Log Files

Let's say you want to search for lines in a log file that contain both the words error and fail (in any order):

$ grep -E 'error.*fail|fail.*error' SNUB_log.txt

This command is useful when you want to find entries in a log file that indicate both an error and a failure, but their order in the line is not important. The command looks for any line containing both terms, regardless of whether "error" comes before "fail" or vice versa.

Let's breakdown this command, it can be confusing:

  • 'error.*fail|fail.*error': This pattern uses:
    • error.*fail: Matches lines where the word "error" appears before "fail", with any characters (.*) in between.
    • fail.*error: Matches lines where the word "fail" appears before "error", with any characters (.*) in between.
    • The | operator means "or", so the line will match either of these patterns.

Search for Multiple Exact Matches in a File

Want to search for specific words like monday, tuesday, or wednesday?

$ grep -E 'monday|tuesday|wednesday' SNUB_file.txt


Bonus: You can also load those patterns from a file:

$ grep -f patterns.txt SNUB_file.txt

Where patterns.txt contains:

monday
tuesday
wednesday

Ignore Case When Grepping for Multiple Strings

Just add the -i flag:

$ grep -iE 'warning|notice|info' SNUB_sys.log

This command searches through the SNUB_sys.log file for lines containing any of the words "warning", "notice", or "info, regardless of case, such as Warning, WARNING, wArNiNg… you get the idea.

Show the Count of Multiple Matches in a File

Sometimes, you don't care about where the match is, just how many there are:

$ grep -oE 'yes|no|maybe' poll_SNUB_results.txt | wc -l

The -o option makes it print only the matched words, one per line, then counted with wc -l.

Grep for Multiple Patterns in a Specific File Type

Searching only .log files in a directory?

$ grep -E 'panic|critical|fatal' *.log

*.log: wildcard tells grep to look in all .log files in the current directory.

This command searches through all files ending in .log in the current directory for lines that contain "panic", "critical", or "fatal".

Search Recursively for Multiple Patterns in a Directory

Use -r or --recursive:

$ grep -rE 'GET|POST|DELETE' /var/log/nginx/

This command searches recursively through Nginx logs for HTTP request types like GET, POST, or DELETE.
This is especially useful for auditing traffic patterns, debugging client behavior, or identifying suspicious activity on your web server.


You can pair it with --include to filter specific file types:

$ grep -rE --include="*.conf" "setting_name" /etc/nginx/

This is perfect for quickly locating configuration settings across all .conf files — whether you're troubleshooting, auditing, or preparing a script.

Clean Output?

Suppress filenames, colorize matches, and show line numbers:

$ grep -nH --color=auto -E 'pattern1|pattern2' SNUB_file.txt

This command is useful when you need to search for multiple patterns, view the results with line numbers, and make the matches stand out by highlighting them in color. Perfect for quickly scanning through logs, configuration files, or code.

Whether you're hunting down errors in log files or filtering specific data from large outputs, mastering multi-string grepping will save you time, energy, and a lot of scrolling.

We hope you found these insights useful!

Keep Us Caffeinated  ⦿ ⦿
Icon Join our 32K+ readers