copied to clipboard!
string grep

Mastering Multiple Conditions in grep: From Basics to Advanced Search Techniques

updated: 2026/05/22 created: 2026/05/22

Introduction

The grep command is an essential search command for working in the terminal on Linux and Mac.

By combining multiple conditions, you can streamline log analysis and configuration checks.

This article clearly explains everything from the basics to practical usage of the grep command for handling multiple conditions.

Reference: GNU grep

How to Specify Multiple Conditions with the grep Command Using Basic Syntax

Create File

cat << 'EOF' > input.txt apple orange banana apple grape banana lemon orange melon apple banana orange EOF

Command

grep 'apple.*banana\|banana.*apple' input.txt

Output

apple orange banana
apple banana orange

Command

grep 'apple' input.txt | grep 'orange'

Output

apple orange banana
apple banana orange

How It Works

Method Example Command How It Works
OR condition grep 'apple.*banana|banana.*apple' input.txt Uses | to specify multiple conditions, extracting lines where apple is followed by banana, or banana is followed by apple
AND condition grep 'apple' input.txt | grep 'orange' Chains grep with a pipe to extract lines containing both apple and orange

Explanation

With the grep command, you can specify OR conditions using \|.

AND conditions can be achieved simply by chaining grep commands with a pipe.

How to Write Multiple grep Conditions on a Single Line

Create File

cat << 'EOF' > input.txt apple red apple green banana yellow grape purple apple yellow banana green EOF

Command

grep -E 'apple|yellow' input.txt

Output

apple red
apple green
banana yellow
apple yellow

Command

grep 'apple' input.txt | grep 'yellow'

Output

apple yellow

How It Works

Method Command Description
OR condition grep -E 'apple|yellow' input.txt Searches for lines containing apple or yellow
AND condition grep 'apple' input.txt | grep 'yellow' Searches for lines containing both apple and yellow
Extended regular expression -E Enables multiple conditions to be handled with regular expressions

Explanation

Using -E with grep allows you to write multiple conditions concisely on a single line.

AND conditions can be achieved by chaining grep with a pipe.

How to Perform OR Searches with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt apple banana orange grape apple pie banana split EOF

Command

grep -E 'apple|banana' input.txt

Output

apple
banana
apple pie
banana split

Command

grep -e 'apple' -e 'banana' input.txt

Output

apple
banana
apple pie
banana split

How It Works

Method Description
grep -E 'apple|banana' Enables extended regular expressions with -E and specifies OR conditions using |
grep -e 'apple' -e 'banana' Achieves OR search by specifying multiple -e options

Explanation

Using OR search with grep allows you to search for multiple keywords at once.

Both -E and -e are commonly used, so choose based on your use case.

How to Perform AND Searches with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt apple orange banana apple grape orange lemon apple orange banana lemon EOF

Command

grep "apple" input.txt | grep "orange"

Output

apple orange banana
apple orange

How It Works

Mechanism Description
First grep grep "apple" extracts lines containing apple
Pipe | Passes the result of the first grep to the next grep
Second grep grep "orange" further extracts lines containing orange
AND search Only lines matching both conditions are displayed

Explanation

By combining grep with pipes, you can perform AND searches with multiple conditions.

Because you can narrow down results step by step with multiple keywords, this is very convenient for log analysis and text searches.

How to Perform NOT Searches with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt apple orange banana apple grape melon banana lemon peach orange melon kiwi grape peach apple EOF

Command

grep -vE 'apple|banana' input.txt

Output

orange melon kiwi

How It Works

Item Description
grep Command for searching text
-v Displays lines that do not match the condition (NOT search)
-E Enables extended regular expressions
apple|banana Specifies the condition for lines containing apple or banana
Processing Excludes lines containing apple or banana and displays the rest

Explanation

Using grep -vE allows you to perform NOT searches with multiple conditions concisely.

The key is combining OR conditions using | with -v.

How to Specify Multiple Conditions Using the -e Option with the grep Command

Create File

cat << 'EOF' > input.txt apple orange banana grape apple lemon banana melon peach orange kiwi apple EOF

Command

grep -e "apple" -e "banana" input.txt

Output

apple orange banana
grape apple lemon
banana melon peach
orange kiwi apple

Command

grep -e "orange" -e "grape" input.txt

Output

apple orange banana
grape apple lemon
orange kiwi apple

How It Works

Item Description
grep Command for searching strings within a file
-e Option for specifying a search condition
Multiple specification Writing multiple -e flags results in an OR condition search
Target file Searches the contents of input.txt

Explanation

Using multiple grep -e flags allows you to search for multiple conditions at once.
This is a commonly used method when you want to search for strings with OR conditions.

How to Search with Multiple Conditions Using Extended Regular Expressions with the -E Option in grep

Create File

cat << 'EOF' > input.txt apple banana orange apple pie banana split grape EOF

Command

grep -E 'apple|banana' input.txt

Output

apple
banana
apple pie
banana split

Command

grep -E '^apple|split$' input.txt

Output

apple
apple pie
banana split

How It Works

Item Description
grep Command for searching text
-E Enables extended regular expressions
apple|banana Matches apple or banana
^apple Searches for lines beginning with apple
split$ Searches for lines ending with split

Explanation

Using grep -E allows you to write OR conditions concisely.

Combining | with ^ and $ enables flexible extraction in multi-condition searches.

How to Search Flexibly with Multiple Conditions Using Regular Expression Patterns in grep

Create File

cat << 'EOF' > input.txt ERROR: database connection failed INFO: user login success WARNING: disk space low ERROR: timeout occurred DEBUG: cache refreshed INFO: backup completed WARNING: memory usage high EOF

Command

grep -E 'ERROR|WARNING' input.txt

Output

ERROR: database connection failed
WARNING: disk space low
ERROR: timeout occurred
WARNING: memory usage high

Command

grep -E 'ERROR.*timeout|INFO.*backup' input.txt

Output

ERROR: timeout occurred
INFO: backup completed

Command

grep -E '^(ERROR|INFO)' input.txt

Output

ERROR: database connection failed
INFO: user login success
ERROR: timeout occurred
INFO: backup completed

How It Works

Item Description
grep -E Uses extended regular expressions
| Specifies OR conditions
.* Represents any string
^ Represents a match at the beginning of a line
( ) Groups conditions

Explanation

Using grep -E enables flexible searches combining multiple conditions.
This is an extremely useful method for log analysis and extracting specific patterns.

How to Narrow Down Results by Combining Multiple grep Conditions with Pipes

Create File

cat << 'EOF' > input.txt ERROR user01 login failed INFO user02 login success ERROR user03 timeout WARN user01 disk usage high ERROR user02 login failed INFO user03 logout EOF

Command

grep "ERROR" input.txt | grep "failed"

Output

ERROR user01 login failed
ERROR user02 login failed

Command

grep "ERROR" input.txt | grep "user02"

Output

ERROR user02 login failed

Command

grep -E "ERROR|WARN" input.txt | grep "user01"

Output

ERROR user01 login failed
WARN user01 disk usage high

How It Works

Command How It Works
grep "ERROR" input.txt | grep "failed" The first grep extracts ERROR lines, and the second narrows them down to lines containing failed
grep "ERROR" input.txt | grep "user02" Extracts lines containing ERROR and further filters to lines containing user02
grep -E "ERROR|WARN" input.txt | grep "user01" Uses -E for OR conditions to get ERROR or WARN lines, then limits to those containing user01

Explanation

By combining the grep command with pipes (|), you can narrow down results step by step with multiple conditions.
This is a very practical method commonly used in log analysis and error investigation.

How to Manage Multiple grep Conditions in Bulk Using the -f Option

Create File

cat << 'EOF' > input.txt ERROR: database connection failed INFO: user login success WARNING: disk usage high ERROR: timeout occurred INFO: batch process completed WARNING: memory usage high EOF

Create File

cat << 'EOF' > patterns.txt ERROR WARNING EOF

Command

grep -f patterns.txt input.txt

Output

ERROR: database connection failed
WARNING: disk usage high
ERROR: timeout occurred
WARNING: memory usage high

How It Works

Item Description
grep Command for searching text
-f patterns.txt Loads search conditions in bulk from a file
patterns.txt Contains multiple search keywords, one per line
input.txt The file to be searched

Explanation

Using grep -f allows you to manage multiple conditions in a separate file, making it easy to add conditions and perform maintenance.
This method is highly readable even when handling a large number of search patterns, and is frequently used in real-world work.

How to Perform Exact Match Searches with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt apple apple pie banana grape pineapple apple EOF

Command

grep -E '^apple$|^banana$' input.txt

Output

apple
banana
apple

Command

grep -wE 'apple|banana' input.txt

Output

apple
apple pie
banana
apple

How It Works

Method Command How It Works
Exact match search grep -E '^apple$|^banana$' input.txt ^ represents the beginning of a line and $ represents the end, searching only for lines that exactly match apple or banana.
Word match search grep -wE 'apple|banana' input.txt The -w option matches at the word boundary level, so lines like apple pie are also included in the search.

Explanation

When performing exact match searches with multiple conditions in grep, using ^ and $ is the most reliable approach.

When word-level matching is sufficient, the -w option allows for concise notation.

How to Count Matches When Searching with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt error: database connection failed info: user login success warning: disk usage high error: timeout occurred info: backup completed warning: memory usage high error: invalid request EOF

Command

grep -E 'error|warning' input.txt

Output

error: database connection failed
warning: disk usage high
error: timeout occurred
warning: memory usage high
error: invalid request

Command

grep -E 'error|warning' input.txt | wc -l

Output

5

How It Works

Command How It Works
grep -E 'error|warning' input.txt Searches for lines containing error or warning
wc -l Counts the number of lines in the grep result and displays the match count

Explanation

Using grep -E allows you to write multiple conditions concisely.

Combining it with wc -l lets you efficiently check the number of matches.

How to Search Multiple Files with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > file1.txt error: connection failed info: retry started warning: timeout detected error: disk full EOF

Create File

cat << 'EOF' > file2.txt info: process started warning: memory usage high error: permission denied success: completed EOF

Command

grep -E 'error|warning' file1.txt file2.txt

Output

file1.txt:error: connection failed
file1.txt:warning: timeout detected
file1.txt:error: disk full
file2.txt:warning: memory usage high
file2.txt:error: permission denied

Command

grep -e 'error' -e 'warning' file1.txt file2.txt

Output

file1.txt:error: connection failed
file1.txt:warning: timeout detected
file1.txt:error: disk full
file2.txt:warning: memory usage high
file2.txt:error: permission denied

How It Works

Item Description
-E Uses extended regular expressions to specify multiple conditions such as error|warning
-e Option that allows defining multiple search conditions
file1.txt file2.txt Specifies multiple files as search targets simultaneously
Output format Displayed in the format filename:matched line

Explanation

grep can search multiple files simultaneously and conditions can be added flexibly.

By choosing between -E and -e appropriately, you can execute multi-condition searches efficiently.

How to Run Recursive Multi-Condition Searches with the grep Command

Create File

cat << 'EOF' > input.txt server=web01 status=active region=tokyo server=web02 status=inactive region=osaka server=db01 status=active region=tokyo server=app01 status=active region=nagoya server=db02 status=inactive region=tokyo EOF

Command

grep -rE 'active.*tokyo|tokyo.*active' .

Output

./input.txt:server=web01 status=active region=tokyo
./input.txt:server=db01 status=active region=tokyo
./input.txt:server=db02 status=inactive region=tokyo

Command

grep -r 'active' . | grep 'tokyo'

Output

./input.txt:server=web01 status=active region=tokyo
./input.txt:server=db01 status=active region=tokyo
./input.txt:server=db02 status=inactive region=tokyo

How It Works

Item Description
grep -r Recursively searches within a directory and its subdirectories
-E Enables extended regular expressions
active.*tokyo Searches for lines where active is followed by tokyo
tokyo.*active Searches for lines where tokyo is followed by active
grep 'active' | grep 'tokyo' AND search using multiple conditions chained with a pipe

Explanation

Using grep -r allows searching including subdirectories.

Multiple conditions can be specified flexibly using regular expressions or pipes.

Common Mistakes and Cautions When Searching with Multiple Conditions Using the grep Command

Create File

cat << 'EOF' > input.txt ERROR: disk full INFO: service started WARNING: high memory usage ERROR: permission denied INFO: backup completed WARNING: cpu temperature high EOF

Command

grep -E 'ERROR|WARNING' input.txt

Output

ERROR: disk full
WARNING: high memory usage
ERROR: permission denied
WARNING: cpu temperature high

Command

grep 'ERROR|WARNING' input.txt

Output

(no results)

Command

grep -e 'ERROR' -e 'WARNING' input.txt

Output

ERROR: disk full
WARNING: high memory usage
ERROR: permission denied
WARNING: cpu temperature high

How It Works

Command How It Works Common Mistake Notes
grep -E 'ERROR|WARNING' Enables extended regular expressions with -E and performs an OR search Forgetting to add -E
grep 'ERROR|WARNING' Processed as a standard regular expression Assuming it performs an OR search
grep -e 'ERROR' -e 'WARNING' Searches with multiple specified conditions Trying to write multiple conditions without -e High readability and a safe way to write multiple conditions

Explanation

When performing multi-condition searches with grep, forgetting -E is a very common mistake.
When you want to write conditions safely, using multiple -e flags is also a widely used approach in real-world work.

Practical Examples of Using Multiple Condition Searches with the grep Command in Real Work

Create File

cat << 'EOF' > input.txt 2026-05-01 INFO User login success 2026-05-01 ERROR Database connection failed 2026-05-02 WARN Disk usage 85% 2026-05-02 ERROR Timeout occurred 2026-05-03 INFO Backup completed 2026-05-03 ERROR Memory leak detected EOF

Command

grep -E 'ERROR|WARN' input.txt

Output

2026-05-01 ERROR Database connection failed
2026-05-02 WARN Disk usage 85%
2026-05-02 ERROR Timeout occurred
2026-05-03 ERROR Memory leak detected

Command

grep 'ERROR' input.txt | grep 'Timeout'

Output

2026-05-02 ERROR Timeout occurred

Command

grep -e 'ERROR' -e 'INFO' input.txt

Output

2026-05-01 INFO User login success
2026-05-01 ERROR Database connection failed
2026-05-02 ERROR Timeout occurred
2026-05-03 INFO Backup completed
2026-05-03 ERROR Memory leak detected

How It Works

Command How It Works Use Case
grep -E 'ERROR|WARN' OR condition search with extended regular expressions Extracting multiple types of log entries
grep 'ERROR' | grep 'Timeout' AND condition search using a pipe Narrowing down specific errors
grep -e 'ERROR' -e 'INFO' Multiple condition specification with -e Easy-to-extend searches

Explanation

Multi-condition searches with grep are frequently used in incident analysis and log monitoring.
By using OR and AND searches appropriately, you can efficiently extract the information you need.

Key Points for Understanding and Effectively Using Multiple Condition Searches in grep

Once you can handle multiple conditions with grep, you go beyond simple string searches and can quickly extract only the information you need.

By using OR, AND, and NOT searches appropriately, you can improve search accuracy.

It is important to check and try each basic syntax one by one.

Mastering multiple condition searches with the grep command can greatly improve your day-to-day work efficiency.

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 running terminal commands