Introduction
grep is a representative command for string searching on Linux and macOS.
In particular, being able to handle multiple patterns allows you to streamline log analysis and configuration file verification.
This article explains grep's multiple pattern search from the basics to practical use in an easy-to-understand way.
Reference: GNU grep
How to Write Basic Searches Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
apple orange banana
grape apple melon
banana kiwi orange
melon grape lemon
EOF
Command to Run
grep -E 'apple|banana' input.txt
Output
apple orange banana
grape apple melon
banana kiwi orange
Command to Run
grep -e 'orange' -e 'grape' input.txt
Output
apple orange banana
grape apple melon
banana kiwi orange
melon grape lemon
How It Works
| Syntax | How It Works | Example |
|---|---|---|
| grep -E 'A|B' | OR search using multiple patterns separated by | | grep -E 'apple|banana' input.txt |
| grep -e pattern1 -e pattern2 | Search by specifying multiple -e flags | grep -e 'orange' -e 'grape' input.txt |
Explanation
grep allows multiple conditions to be treated as an OR search.
Using -E with regular expressions and specifying multiple -e flags are the fundamental approaches.
How to OR Search Multiple Strings Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
apple
banana
orange
grape
pineapple
EOF
Command to Run
grep -E 'apple|orange' input.txt
Output
apple
orange
pineapple
Command to Run
grep -e 'banana' -e 'grape' input.txt
Output
banana
grape
How It Works
| Method | Example Command | How It Works |
|---|---|---|
| Using -E | grep -E 'apple|orange' input.txt | Specify OR conditions using | |
| Using multiple -e flags | grep -e 'banana' -e 'grape' input.txt | Add search conditions with each -e flag and display lines matching any of them |
Explanation
With grep, using -E and | allows concise OR searches.
Also, specifying multiple -e flags enables multi-condition searches with high readability.
How to Use and Distinguish Between the -E Option and egrep for Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
error: disk full
warning: cpu high
info: backup completed
error: memory leak
warning: network unstable
EOF
Command to Run
grep -E 'error|warning' input.txt
Output
error: disk full
warning: cpu high
error: memory leak
warning: network unstable
Command to Run
egrep 'error|warning' input.txt
Output
error: disk full
warning: cpu high
error: memory leak
warning: network unstable
How It Works
| Method | Description | Notes |
|---|---|---|
| grep -E | Enables extended regular expressions | Currently the recommended approach |
| egrep | Shorthand for grep -E | May be deprecated in some modern environments |
| error|warning | Search for multiple patterns with OR condition |
Explanation
grep -E leverages extended regular expressions to search for multiple patterns concisely.
egrep offers equivalent functionality, but grep -E is now the more common choice.
How to Perform Exact Match Searches Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
apple
banana
orange
grape
melon
EOF
Creating the File
cat << 'EOF' > patterns.txt
apple
orange
melon
EOF
Command to Run
grep -x -E 'apple|orange|melon' input.txt
Output
apple
orange
melon
Command to Run
grep -x -f patterns.txt input.txt
Output
apple
orange
melon
How It Works
| Option | Description |
|---|---|
| -x | Perform exact match search on the entire line |
| -E | Use extended regular expressions |
| apple|orange|melon | Specify multiple patterns with OR condition |
| -f patterns.txt | Load patterns from a file |
Explanation
Using grep -x allows you to perform exact match searches rather than partial matches.
When there are many search patterns, using patterns.txt makes them easier to manage.
How to Match Only Whole Words Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
apple
pineapple
banana
orange
apple pie
banana split
grape
EOF
Command to Run
grep -Ew 'apple|banana' input.txt
Output
apple
banana
apple pie
banana split
Command to Run
grep -E 'apple|banana' input.txt
Output
apple
pineapple
banana
apple pie
banana split
How It Works
| Option | Description | Behavior |
|---|---|---|
| -E | Use extended regular expressions | Enables OR conditions like apple|banana |
| -w | Match whole words only | Treats surrounding characters as word boundaries |
| apple|banana | Specify multiple patterns | Matches apple or banana |
Explanation
Using grep -Ew lets you search for multiple patterns with OR conditions while matching only whole words.
This is useful when you want to exclude partial matches like pineapple.
How to Search by Specifying Start-of-Line and End-of-Line Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
ERROR: failed to connect
INFO: retry started
ERROR: timeout detected
DEBUG: cache cleared
INFO: process completed
WARNING: disk usage high
EOF
Command to Run
grep -E '^(ERROR|INFO)|high$' input.txt
Output
ERROR: failed to connect
INFO: retry started
ERROR: timeout detected
INFO: process completed
WARNING: disk usage high
How It Works
| Item | Description |
|---|---|
| -E | Enable extended regular expressions |
| ^ | Match start of line |
| $ | Match end of line |
| (ERROR|INFO) | Specify multiple patterns with OR condition |
| high$ | Search for lines ending with "high" |
Explanation
Using grep -E allows you to specify multiple patterns concisely.
Combining ^ and $ enables flexible searches conditioned on the start or end of a line.
How to Search Without Case Distinction Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
Hello World
hello grep
HELLO LINUX
Error Message
warning message
EOF
Command to Run
grep -Ei 'hello|error' input.txt
Output
Hello World
hello grep
HELLO LINUX
Error Message
How It Works
| Item | Description |
|---|---|
| grep | Command for text searching |
| -E | Enable extended regular expressions, allowing OR conditions without escaping | |
| -i | Search without distinguishing uppercase and lowercase |
| hello|error | Multiple patterns matching hello or error |
| input.txt | Target file for search |
Explanation
Using -E allows multiple patterns to be written simply.
Combining with -i enables flexible searches regardless of uppercase or lowercase.
How to Perform AND Searches Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
apple orange banana
apple grape
orange lemon
apple orange
banana grape
EOF
Command to Run
grep 'apple' input.txt | grep 'orange'
Output
apple orange banana
apple orange
How It Works
| Mechanism | Description |
|---|---|
| First grep | Extracts lines containing "apple" |
| Pipe | | Passes the result of the previous command to the next command |
| Second grep | Further extracts only lines containing "orange" |
| AND search | Displays only lines matching both conditions |
Explanation
grep can achieve AND searches for multiple conditions by chaining with pipes.
This approach allows step-by-step filtering with multiple keywords and is commonly used in log analysis.
How to Combine NOT Searches Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
error: database connection failed
info: retry connection
warning: disk usage high
error: timeout occurred
debug: cache initialized
fatal: unexpected shutdown
EOF
Command to Run
grep -Ev 'error|warning' input.txt
Output
info: retry connection
debug: cache initialized
fatal: unexpected shutdown
Command to Run
grep -v 'error' input.txt | grep -v 'warning'
Output
info: retry connection
debug: cache initialized
fatal: unexpected shutdown
How It Works
| Mechanism | Example Command | Description |
|---|---|---|
| Specify multiple NOT conditions at once | grep -Ev 'error|warning' input.txt | Use extended regular expressions to exclude lines containing error or warning |
| Chain NOT conditions | grep -v 'error' | grep -v 'warning' | Chain grep -v with pipes to exclude step by step |
Explanation
grep -v is the option for NOT searches.
When handling multiple conditions, combining -E with | is the simplest and most practical approach.
How to Load Patterns from a File Using the -f Option with grep
Creating the File
cat << 'EOF' > patterns.txt
error
warning
failed
EOF
Creating the File
cat << 'EOF' > input.txt
[INFO] process started
[WARNING] disk usage high
[ERROR] connection failed
[SUCCESS] backup completed
failed to connect database
EOF
Command to Run
grep -i -f patterns.txt input.txt
Output
[WARNING] disk usage high
[ERROR] connection failed
failed to connect database
How It Works
| Item | Description |
|---|---|
| -f patterns.txt | Load search patterns from a file |
| -i | Search without distinguishing uppercase and lowercase |
| patterns.txt | Define multiple search keywords one per line |
| input.txt | Target file for search |
| Behavior | Extract lines matching any of the patterns in patterns.txt |
Explanation
Using grep -f makes multiple patterns easier to manage and is convenient for large-scale keyword searches.
Externalizing patterns into a file also improves maintainability and reusability.
How to Search Multiple Files Simultaneously Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > sample1.txt
ERROR: Disk full
INFO: Service started
WARNING: CPU usage high
ERROR: Connection timeout
EOF
Creating the File
cat << 'EOF' > sample2.txt
INFO: Backup completed
WARNING: Memory usage high
ERROR: Failed login attempt
INFO: System shutdown
EOF
Command to Run
grep -E 'ERROR|WARNING' sample1.txt sample2.txt
Output
sample1.txt:ERROR: Disk full
sample1.txt:WARNING: CPU usage high
sample1.txt:ERROR: Connection timeout
sample2.txt:WARNING: Memory usage high
sample2.txt:ERROR: Failed login attempt
Command to Run
grep -e 'ERROR' -e 'WARNING' sample1.txt sample2.txt
Output
sample1.txt:ERROR: Disk full
sample1.txt:WARNING: CPU usage high
sample1.txt:ERROR: Connection timeout
sample2.txt:WARNING: Memory usage high
sample2.txt:ERROR: Failed login attempt
How It Works
| Method | Description | Notes |
|---|---|---|
| grep -E 'ERROR|WARNING' | Specify multiple patterns with extended regular expressions | Uses | for OR conditions |
| grep -e 'ERROR' -e 'WARNING' | Specify multiple -e flags | Easy to add more patterns |
| Specify multiple files | sample1.txt sample2.txt | Can be searched simultaneously |
| Output format | filename:matched line | Shows which file the match was found in |
Explanation
grep can execute multiple pattern searches and multiple file searches simultaneously.
This is an extremely useful approach for log analysis and error investigation.
How to Recursively Search Directories Using Multiple Patterns with grep
Creating the File
mkdir -p project/src
Creating the File
cat << 'EOF' > project/app.log
INFO: Application started
ERROR: Database connection failed
WARN: Retry processing
EOF
Creating the File
cat << 'EOF' > project/error.log
FATAL: System crash
ERROR: Memory leak detected
EOF
Creating the File
cat << 'EOF' > project/src/main.txt
INFO: User login success
DEBUG: Session created
EOF
Creating the File
cat << 'EOF' > project/src/debug.txt
TRACE: API request
WARN: Slow response detected
EOF
Command to Run
grep -r -E "ERROR|WARN" project
Output
project/src/debug.txt:WARN: Slow response detected
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected
Command to Run
grep -r -e "ERROR" -e "WARN" project
Output
project/src/debug.txt:WARN: Slow response detected
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected
How It Works
| Option | Role |
|---|---|
| grep | Text search command |
| -r | Recursively search directories |
| -E | Enable extended regular expressions |
| "ERROR|WARN" | Search for multiple patterns with OR condition |
| -e | Specify multiple search patterns |
| project | Target directory for search |
Explanation
With grep, using -E allows OR-condition searches across multiple patterns.
The approach of specifying multiple -e flags is also easy to extend with additional patterns and offers high readability.
How to Exclude Target Directories When Searching Using Multiple Patterns with grep
Creating the File
mkdir -p project/src
Creating the File
cat << 'EOF' > project/app.log
INFO: Application started
ERROR: Database connection failed
WARN: Retry processing
EOF
Creating the File
cat << 'EOF' > project/error.log
FATAL: System crash
ERROR: Memory leak detected
EOF
Creating the File
cat << 'EOF' > project/src/main.txt
INFO: User login success
DEBUG: Session created
EOF
Creating the File
cat << 'EOF' > project/src/debug.txt
TRACE: API request
WARN: Slow response detected
EOF
Command to Run
grep -r -E "ERROR|WARN" --exclude-dir="project/src" project
Output
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected
Command to Run
grep -r -E "ERROR|WARN" project
Output
project/app.log:ERROR: Database connection failed
project/app.log:WARN: Retry processing
project/error.log:ERROR: Memory leak detected
project/src/debug.txt:WARN: Slow response detected
How It Works
| Option | Description |
|---|---|
| -r | Recursively search directories |
| -E | Use extended regular expressions |
| "ERROR|WARN" | Search for lines matching ERROR or WARN |
| --exclude-dir="project/src" | Exclude the project/src directory from the search |
Explanation
The grep command can search for multiple patterns with OR conditions using -E.
Also, using the --exclude-dir option lets you exclude specific directories from the search.
Excluding unnecessary directories during log searches helps improve search speed and reduce noise.
How to Integrate grep Multiple Patterns with the find Command
Creating the File
cat << 'EOF' > input.txt
logs/app.log:ERROR Database connection failed
logs/app.log:INFO User login success
logs/system.log:WARNING Disk usage high
logs/system.log:ERROR Memory leak detected
backup/data.txt:INFO Backup completed
backup/data.txt:ERROR Backup failed
EOF
Creating the File
mkdir -p logs backup
Creating the File
cp input.txt logs/app.log
Creating the File
cp input.txt logs/system.log
Creating the File
cp input.txt backup/data.txt
Command to Run
find logs backup -type f | xargs grep -E 'ERROR|WARNING'
Output
logs/app.log:logs/app.log:ERROR Database connection failed
logs/app.log:logs/system.log:WARNING Disk usage high
logs/app.log:logs/system.log:ERROR Memory leak detected
logs/app.log:backup/data.txt:ERROR Backup failed
logs/system.log:logs/app.log:ERROR Database connection failed
logs/system.log:logs/system.log:WARNING Disk usage high
logs/system.log:logs/system.log:ERROR Memory leak detected
logs/system.log:backup/data.txt:ERROR Backup failed
backup/data.txt:logs/app.log:ERROR Database connection failed
backup/data.txt:logs/system.log:WARNING Disk usage high
backup/data.txt:logs/system.log:ERROR Memory leak detected
backup/data.txt:backup/data.txt:ERROR Backup failed
How It Works
| Command | Role |
|---|---|
| find logs backup -type f | Find files under logs and backup directories |
| xargs grep -E 'ERROR|WARNING' | Run grep search with multiple patterns in OR condition |
| -E | Enable extended regular expressions |
| ERROR|WARNING | Match ERROR or WARNING |
Explanation
By limiting the target directories with find, you can efficiently run grep searches while avoiding unnecessary file scanning.
Using grep -E allows multiple keywords to be searched together with OR conditions.
How to Streamline Log Analysis Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
2026-05-20 10:00:01 INFO User login success
2026-05-20 10:01:15 ERROR Database connection failed
2026-05-20 10:02:30 WARN Disk usage above 80%
2026-05-20 10:03:45 INFO Scheduled backup completed
2026-05-20 10:04:50 ERROR Timeout while calling API
2026-05-20 10:05:20 WARN Memory usage high
EOF
Command to Run
grep -E 'ERROR|WARN' input.txt
Output
2026-05-20 10:01:15 ERROR Database connection failed
2026-05-20 10:02:30 WARN Disk usage above 80%
2026-05-20 10:04:50 ERROR Timeout while calling API
2026-05-20 10:05:20 WARN Memory usage high
Command to Run
grep -e 'ERROR' -e 'WARN' input.txt
Output
2026-05-20 10:01:15 ERROR Database connection failed
2026-05-20 10:02:30 WARN Disk usage above 80%
2026-05-20 10:04:50 ERROR Timeout while calling API
2026-05-20 10:05:20 WARN Memory usage high
How It Works
| Method | Example Command | How It Works |
|---|---|---|
| -E option | grep -E 'ERROR|WARN' input.txt | Use extended regular expressions with | to specify multiple conditions |
| -e option | grep -e 'ERROR' -e 'WARN' input.txt | Specify multiple conditions by using multiple -e flags |
Explanation
Using multiple patterns with grep allows you to extract multiple conditions from large logs all at once.
This is extremely effective for streamlining failure analysis and monitoring log review.
Common Errors and Solutions When Using Multiple Patterns with grep
Creating the File
cat << 'EOF' > input.txt
error: failed login
warning: disk usage high
info: backup completed
error: timeout detected
warning: memory leak suspected
EOF
Command to Run
grep -E 'error|warning' input.txt
Output
error: failed login
warning: disk usage high
error: timeout detected
warning: memory leak suspected
Command to Run
grep 'error|warning' input.txt
Output
(no output)
Command to Run
grep -e 'error' -e 'warning' input.txt
Output
error: failed login
warning: disk usage high
error: timeout detected
warning: memory leak suspected
How It Works
| Mechanism | Description |
|---|---|
| grep -E 'error|warning' | Enable extended regular expressions and perform OR search using | |
| grep 'error|warning' | | is not interpreted as OR in standard grep |
| grep -e 'error' -e 'warning' | Achieve OR search by specifying multiple -e flags |
Explanation
One of the most common mistakes with grep's multiple patterns is forgetting -E when using |.
If you want to ensure reliable behavior, using multiple grep -e flags is also a widely used approach.
Summary of Key Tips for Efficient grep Multiple Pattern Searches
Understanding grep's multiple pattern search allows you to flexibly handle OR searches, AND searches, and exclusion searches.
Furthermore, combining with the find command and recursive search makes large-scale log analysis and configuration review more efficient.
Start by trying the basic pattern specifications, and learn to choose the right options for each use case.

