copied to clipboard!
string grep

Mastering grep: A Comprehensive Guide to AND Conditions

updated: 2026/05/27 created: 2026/05/27

Introduction

grep is a standard command for string searching on Linux and Mac.

In particular, learning to use AND conditions can greatly improve efficiency when analyzing logs or searching across multiple files.

This article organizes grep AND conditions in an easy-to-understand way for beginners, and explains search methods that are useful in practice.

Reference: GNU grep

Basic Syntax for grep AND Conditions

Creating the 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

Item Description
First grep Extracts lines containing apple
Pipe | Passes the result of the previous command to the next command
Second grep Further extracts lines containing orange
AND condition Displays only lines that contain both strings

Explanation

The AND condition in grep can be achieved by combining multiple grep commands with a pipe. The basic mechanism is to further narrow down the results of the first search in the second stage.

How to Specify Multiple Keywords with grep AND Conditions

Creating the File

cat << 'EOF' > input.txt apple orange banana apple grape orange lemon apple orange banana grape 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 previous search result to the next grep
Second grep grep "orange" further extracts only lines containing orange
AND condition AND search is achieved by chaining multiple grep commands

Explanation

grep is often used with OR conditions on its own, but connecting it with a pipe achieves AND conditions.

By narrowing down multiple keywords in sequence, you can efficiently extract only the target lines.

How to Achieve grep AND Conditions Using a Pipe

Creating the File

cat << 'EOF' > input.txt apple red fresh apple green sour banana yellow fresh orange orange sweet apple red sweet EOF

Command

grep 'apple' input.txt | grep 'red'

Output

apple red fresh
apple red sweet

How It Works

Item Description
First grep grep 'apple' input.txt extracts lines containing apple
Pipe | Passes the previous grep result to the next grep
Second grep grep 'red' further extracts only lines containing red
Result AND condition containing both apple and red is achieved

Explanation

Chaining grep with a pipe allows you to add further conditions to the previous search results.
This method is simple and highly readable, as multiple conditions can be narrowed down step by step.

How to Achieve grep AND Conditions Using Regular Expressions

Creating the File

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

Command

grep -E 'apple.*orange|orange.*apple' input.txt

Output

apple orange banana
apple orange

How It Works

Item Description
grep Command for text searching
-E Enables extended regular expressions
apple.*orange Searches for lines where orange appears after apple
orange.*apple Searches for lines where apple appears after orange
| Represents an OR condition in regular expressions
Achieving AND condition Achieved by combining order-independent patterns with OR

Explanation

grep itself has no AND operator, but AND conditions can be achieved by combining regular expressions.
The key is to specify both orderings as an OR condition to handle any order of the keywords.

How to Search for Lines Containing Multiple Strings in One Line Using grep AND Conditions

Creating the File

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

Command

grep 'apple' input.txt

Output

apple orange banana
apple grape
apple orange

Command

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 search result to the next command
Second grep Further extracts only lines containing orange
AND condition Displays only lines that contain both strings

Explanation

grep can achieve AND conditions by combining it with a pipe.

Multi-condition searches are commonly used in log analysis and configuration file investigation.

Differences Between grep AND Conditions and OR Conditions

Creating the File

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

Command

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

Output

apple yellow

Command

grep -E 'apple|banana' input.txt

Output

apple red
apple green
banana yellow
apple yellow

How It Works

Condition Example Command Mechanism Result
AND condition grep 'apple' input.txt | grep 'yellow' The first grep result is further narrowed down by the second grep Lines containing both apple and yellow
OR condition grep -E 'apple|banana' input.txt Extended regular expressions are enabled with -E and multiple conditions are specified with | Lines containing apple or banana

Explanation

The grep AND condition is achieved by running grep multiple times using a pipe |.
On the other hand, OR conditions can be written concisely using grep -E and |.

How to Combine grep AND Conditions with NOT Conditions

Creating the File

cat << 'EOF' > input.txt error: database connection failed info: user login success warning: disk usage high error: timeout occurred debug: retry process started EOF

Command

grep "error" input.txt | grep -v "timeout"

Output

error: database connection failed

How It Works

Condition Command Description
AND condition grep "error" input.txt | grep "failed" Extracts lines containing both error and failed
NOT condition grep -v "timeout" Excludes lines containing timeout
AND + NOT condition grep "error" input.txt | grep -v "timeout" Extracts lines containing error but not timeout

Explanation

grep can achieve AND conditions by combining multiple conditions with a pipe (|).

Using grep -v works as a NOT condition to exclude lines containing a specific string.

How to Specify Multiple Conditions with the -e Option in grep AND Conditions

Creating the File

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

Command

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

Output

apple orange banana
apple grape
orange lemon
apple orange

Command

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

Output

apple orange banana
apple grape
apple orange

Command

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

Output

apple orange banana
apple orange

How It Works

Item Description
-e option Allows specifying multiple search patterns
grep -e "apple" -e "orange" Extracts lines containing apple or orange
grep "apple" Narrows down to only lines containing apple
grep "orange" Narrows down to only lines containing orange
Achieving AND condition Achieved by chaining grep commands with a pipe to match both

Explanation

Using the -e option with grep allows flexible specification of multiple conditions.

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

How to Search Multiple Files with grep AND Conditions

Creating the File

cat << 'EOF' > file1.txt ERROR: database connection failed INFO: retry started WARN: timeout detected EOF

Creating the File

cat << 'EOF' > file2.txt INFO: process started ERROR: timeout while reading response INFO: process finished EOF

Creating the File

cat << 'EOF' > file3.txt WARN: disk usage high ERROR: timeout detected in backup EOF

Command

grep -l "ERROR" file*.txt | xargs grep -l "timeout"

Output

file1.txt
file2.txt
file3.txt

How It Works

Mechanism Description
grep -l "ERROR" file*.txt Extracts only filenames containing ERROR
xargs grep -l "timeout" Further searches for timeout in the extracted files
AND condition AND search is achieved by applying two grep conditions in sequence

Explanation

Since grep alone has no AND operator, search results are passed to the next grep to achieve it.

For searching multiple files, combining xargs allows efficient narrowing down.

How to Recursively Search a Directory with grep AND Conditions

Creating the File

cat << 'EOF' > input.txt project/app.log:ERROR Database connection failed project/app.log:INFO User login success project/server.log:ERROR Timeout occurred project/server.log:WARN Memory usage high project/debug.log:ERROR Disk full project/debug.log:INFO Retry process started EOF

Creating the File

mkdir -p project && while IFS=: read -r file content; do echo "$content" >> "$file"; done < input.txt

Command

grep -r "ERROR" project | grep "Timeout"

Output

project/server.log:ERROR Timeout occurred

How It Works

Item Description
grep -r Recursively searches under a directory
grep "ERROR" Extracts lines containing ERROR
| grep "Timeout" Further extracts only lines containing Timeout
AND condition Achieved by chaining multiple grep commands with a pipe

Explanation

grep alone is primarily used for OR searches, but chaining it with a pipe enables AND condition searching.

Using grep -r allows you to search all files under a directory at once.

How to Analyze Logs with grep AND Conditions

Creating the File

cat << 'EOF' > input.txt 2026-05-26 10:00:01 INFO User login success 2026-05-26 10:01:15 ERROR Database connection failed 2026-05-26 10:02:30 ERROR Timeout while connecting API 2026-05-26 10:03:45 INFO Scheduled batch started 2026-05-26 10:04:12 ERROR Database timeout detected 2026-05-26 10:05:20 WARN Disk usage high EOF

Command

grep 'ERROR' input.txt | grep 'Database'

Output

2026-05-26 10:01:15 ERROR Database connection failed
2026-05-26 10:04:12 ERROR Database timeout detected

Command

grep -E 'ERROR.*Database|Database.*ERROR' input.txt

Output

2026-05-26 10:01:15 ERROR Database connection failed
2026-05-26 10:04:12 ERROR Database timeout detected

How It Works

Method Command Mechanism
Chaining grep with pipe grep 'ERROR' input.txt | grep 'Database' The first grep result is further narrowed down by the second grep, achieving AND condition
Using regular expressions grep -E 'ERROR.*Database|Database.*ERROR' input.txt Extracts lines where both keywords exist in one line

Explanation

The simplest way to apply grep AND conditions is to run grep multiple times with a pipe.
When you want to check whether multiple keywords exist in one line, regular expressions are also handy.

How to Combine grep AND Conditions with the find Command

Creating the File

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

Command

find . -name "input.txt" -type f -exec grep -E "apple" {} \; | grep "yellow"

Output

apple yellow

How It Works

Command Role
find . -name "input.txt" Searches for the target file
grep -E "apple" Narrows down with the first condition
grep "yellow" Further narrows down with the second condition
| (pipe) Passes the grep result to the next grep to achieve AND condition

Explanation

Since grep alone is not well-suited for AND conditions directly, multiple grep commands are chained with a pipe.

Combining with find allows flexible AND condition searching even when searching multiple files.

How to Efficiently Analyze Large Volumes of Logs with grep AND Conditions

Creating the File

cat << 'EOF' > input.txt 2026-05-26 10:00:01 INFO user_id=101 status=SUCCESS action=login 2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment 2026-05-26 10:00:08 ERROR user_id=103 status=FAILED action=login 2026-05-26 10:00:10 INFO user_id=104 status=SUCCESS action=logout 2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment 2026-05-26 10:00:20 WARN user_id=106 status=RETRY action=payment EOF

Command

grep "ERROR" input.txt | grep "payment"

Output

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

Command

grep -E "ERROR.*payment" input.txt

Output

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

Command

awk '/ERROR/ && /payment/' input.txt

Output

2026-05-26 10:00:05 ERROR user_id=102 status=FAILED action=payment
2026-05-26 10:00:15 ERROR user_id=105 status=FAILED action=payment

How It Works

Method Example Command Mechanism Feature
grep pipe grep "ERROR" input.txt | grep "payment" The first grep result is narrowed down by the second Simple and easy to understand
grep regular expression grep -E "ERROR.*payment" input.txt AND condition evaluated in a single grep using regular expressions Fast and suitable for large logs
awk condition awk '/ERROR/ && /payment/' input.txt Multiple conditions evaluated with awk's logical AND Flexible when adding conditions

Explanation

With grep AND conditions, using a pipe or regular expressions allows efficient extraction of multiple keywords.

For large log analysis, using grep -E or awk reduces the number of processing steps and is faster.

Advanced grep AND Condition Techniques

Creating the File

cat << 'EOF' > input.txt error: disk full info: backup completed warning: cpu temperature high error: memory leak detected info: user login success warning: network unstable EOF

Command

grep 'error' input.txt | grep 'memory'

Output

error: memory leak detected

Command

grep -E 'error.*memory|memory.*error' input.txt

Output

error: memory leak detected

Command

grep 'warning' input.txt | grep 'network'

Output

warning: network unstable

How It Works

Command Mechanism
grep 'error' input.txt | grep 'memory' The first grep extracts lines containing error, then the second grep narrows down to lines containing memory from those results
grep -E 'error.*memory|memory.*error' input.txt Uses extended regular expressions to search for lines containing both error and memory in a single grep
grep 'warning' input.txt | grep 'network' Uses a pipe to perform AND search with multiple conditions

Explanation

The basic approach for grep AND conditions is to chain multiple grep commands with a pipe.
Additionally, using grep -E enables flexible condition specification with regular expressions.

Summary: Understanding grep AND Conditions to Improve Search Efficiency

grep AND conditions are a convenient technique that lets you combine multiple conditions to extract only the information you need.

By learning everything from the basic pipe-based approach to combinations with regular expressions and the find command, you can greatly improve efficiency in log analysis and troubleshooting tasks.

For beginners, the best learning approach is to start with simple AND conditions and gradually expand to more advanced uses such as multi-file searching and recursive searching.

Leave a Reply

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

©︎ 2025-2026 running terminal commands