copied to clipboard!
string grep

Mastering Grep and the Pipe Operator

updated: 2026/08/02 created: 2026/08/02

Introduction

grep and pipe are indispensable commands for efficient string searching on Linux and Unix-like operating systems.

Using a pipe lets you chain multiple commands together.

This article explains everything from the basic syntax of grep and pipe to more advanced applications.

Reference: GNU grep

Basic syntax of grep and pipe

Create the file

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

Command to run

cat input.txt | grep "apple"

Output

apple
apple pie
pineapple
apple juice

Command to run

grep "apple" input.txt | grep "juice"

Output

apple juice

How it works

Item Description
cat input.txt Displays the contents of the file to standard output
| (pipe) Passes the output of the previous command as input to the next command
grep "apple" Extracts only the lines containing apple
grep "juice" Further extracts only the lines containing juice from the previous result
Benefit of pipes Lets you combine multiple commands to efficiently narrow down data

Explanation

grep is a command that extracts lines matching a condition.
By combining it with a pipe (|), you can apply multiple conditions in sequence and efficiently extract only the data you need.

How to search standard input with grep and pipe

Create the file

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

Command to run

cat input.txt | grep "apple"

Output

apple
apple pie
pineapple

Command to run

echo -e "dog\ncat\nbird\ncatfish" | grep "cat"

Output

cat
catfish

How it works

Item Description
cat input.txt Displays the contents of the file to standard output
| Passes the standard output of the left-hand command to the standard input of the right-hand command
grep "apple" Extracts only the lines containing apple from standard input
Processing flow Data flows in the order cat → pipe → grep

Explanation

Using a pipe (|), you can feed the output of the previous command directly into grep as input.

Not only files but also output from echo and other commands can be searched the same way, making shell data processing more efficient.

How to search cat command output with grep and pipe

Create the file

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

Command to run

cat input.txt | grep "apple"

Output

apple
pineapple
apple juice

How it works

Element Role
cat input.txt Displays the contents of input.txt to standard output
| (pipe) Passes the standard output of the previous command to the standard input of the next command
grep "apple" Extracts only the lines containing apple from the input text

Explanation

Using a pipe (|), you can feed the output of cat straight into grep for searching.

Combining multiple commands lets you search and process text efficiently.

How to narrow down multiple conditions with grep and pipe

Create the file

cat << 'EOF' > input.txt INFO: Server started ERROR: Disk full INFO: User login ERROR: Network timeout WARNING: High memory usage ERROR: Disk read failure INFO: Backup completed EOF

Command to run

grep "ERROR" input.txt | grep "Disk"

Output

ERROR: Disk full
ERROR: Disk read failure

Command to run

grep "INFO" input.txt | grep "Backup"

Output

INFO: Backup completed

How it works

Command Role
grep "ERROR" input.txt Extracts only the lines containing ERROR
| Passes the output of the previous command to the next (pipe)
grep "Disk" Extracts only the lines containing Disk from the result received via the pipe
grep "INFO" input.txt | grep "Backup" Extracts only the lines containing Backup from among the lines containing INFO

Explanation

By using a pipe (|), you can pass the results of one grep search into another grep, narrowing down step by step using multiple conditions.

Even complex search conditions can be achieved through simple combinations of commands.

How to perform an exclusion search with grep and pipe

Create the file

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

Command to run

grep "a" input.txt | grep -v "banana"

Output

apple
orange
grape
orange juice

How it works

Element Description
grep "a" input.txt Extracts lines containing a
| Passes the result of the previous command to the next (pipe)
grep -v "banana" Excludes lines containing banana
Final result Displays only lines that contain a and do not contain banana

Explanation

Combining grep with a pipe lets you further narrow down search results.

grep -v is often used when you want to exclude lines matching a condition.

How to search flexibly using regular expressions with grep and pipe

Create the file

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

Command to run

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

Output

apple
banana
apple pie
banana split

Command to run

cat input.txt | grep -Ei 'apple|orange'

Output

apple
orange
apple pie
pineapple
Apple

Command to run

cat input.txt | grep -Ev 'apple|banana'

Output

orange
grape
Apple

How it works

Element Description
cat input.txt Outputs the contents of the file to standard output
| Passes the output of the previous command to the next (pipe)
grep Searches for lines matching a condition
-E Enables extended regular expressions, allowing OR searches such as apple|banana
-i Searches without distinguishing between uppercase and lowercase
-v Displays lines that do NOT match, instead of lines that match
^apple Matches lines that start with apple
apple|orange A regular expression matching either apple or orange

Explanation

Using a pipe lets you feed the output of the previous command directly into grep for searching.
Combining grep -E with regular expressions enables flexible conditions such as OR searches and matching the start of a line, all while searching efficiently.

How to combine grep and pipe with head and tail

Create the file

cat << 'EOF' > input.txt error: failed to connect info: server started warning: low memory error: permission denied info: user login error: disk full warning: cpu usage high info: backup completed error: network timeout info: shutdown completed EOF

Command to run

grep "error" input.txt | head -n 2

Output

error: failed to connect
error: permission denied

Command to run

grep "error" input.txt | tail -n 2

Output

error: disk full
error: network timeout

How it works

Element Description
grep "error" input.txt Extracts only the lines containing error
| (pipe) Passes the result of grep to the next command
head -n 2 Displays the first 2 lines received via the pipe
tail -n 2 Displays the last 2 lines received via the pipe

Explanation

The search results from grep can be passed to head or tail using a pipe (|).
This is convenient when you only want to check the beginning or end of a large set of search results.

How to combine grep and pipe with sort and uniq

Create the file

cat << 'EOF' > input.txt INFO Login success ERROR Database error INFO File uploaded WARN Disk space low ERROR Network timeout INFO Login success ERROR Database error INFO Logout WARN Disk space low INFO File uploaded EOF

Command to run

grep "ERROR" input.txt | sort | uniq

Output

ERROR Database error
ERROR Network timeout

Command to run

grep "INFO" input.txt | sort | uniq

Output

INFO File uploaded
INFO Login success
INFO Logout

Command to run

grep -E "INFO|WARN" input.txt | sort | uniq

Output

INFO File uploaded
INFO Login success
INFO Logout
WARN Disk space low

How it works

Command Role
grep Extracts only the lines matching a condition
| (pipe) Passes the result of the previous command to the next command
sort Sorts the extracted lines
uniq Collapses consecutive duplicate lines into one
grep "ERROR" input.txt | sort | uniq Extracts ERROR lines, sorts them, and then removes duplicates

Explanation

By extracting only the needed lines with grep and passing the result to sort via a pipe, you can reorder duplicate lines.
Combining this with uniq at the end lets you display each identical entry only once.

How to combine grep and pipe with wc to count matches

Create the file

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

Command to run

grep "apple" input.txt | wc -l

Output

4

How it works

Element Role
grep "apple" input.txt Extracts only the lines containing apple from input.txt
| Passes the output of grep to the next command (pipe)
wc -l Counts the number of lines passed to it
Result Outputs 4, since there are 4 lines containing apple

Explanation

By extracting matching lines with grep and passing the result to wc -l via a pipe (|), you can easily count the number of matches.
This combination is frequently used for log analysis and text searches.

How to combine grep and pipe with awk

Create the file

cat << 'EOF' > input.txt INFO: Server started ERROR: Disk full INFO: User login ERROR: Connection timeout WARN: High memory usage ERROR: Permission denied EOF

Command to run

grep "ERROR" input.txt | awk '{print $2, $3}'

Output

Disk full
Connection timeout
Permission denied

Command to run

grep "INFO" input.txt | awk '{print NR ":", $2, $3}'

Output

1: Server started
2: User login

How it works

Element Role
grep "ERROR" input.txt Extracts only the lines containing ERROR
awk '{print $2, $3}' Displays only the 2nd and 3rd columns of the extracted lines
NR A built-in awk variable representing the line number currently being processed

Explanation

By extracting only the needed lines with grep and passing the result to awk via a pipe, you can process data efficiently.
This combination is commonly used for log analysis and aggregating text files.

How to further narrow down find command results with grep and pipe

Create the file

cat << 'EOF' > input.txt error: failed to connect info: service started warning: disk usage 80% error: timeout occurred info: backup completed warning: memory usage high error: permission denied EOF

Command to run

find . -name "*.txt" | grep "input"

Output

./input.txt

Command to run

find . -name "*.txt" | grep "input" | xargs grep "error"

Output

./input.txt:error: failed to connect
./input.txt:error: timeout occurred
./input.txt:error: permission denied

How it works

Element Role
find . -name "*.txt" Searches for .txt files under the current directory
| Passes the standard output of the previous command to the next (pipe)
grep "input" Extracts only file names containing "input" from the find results
xargs grep "error" Passes the extracted files to grep and displays only the lines containing "error"

Explanation

By passing the results of find into grep via a pipe, you can efficiently narrow down to just the files you want.
Combining this further with xargs grep also lets you search for a specific string across those target files all at once.

How to efficiently analyze log files with grep and pipe

Create the file

cat << 'EOF' > input.txt 2026-08-01 10:00:01 INFO Application started 2026-08-01 10:00:15 INFO User login: alice 2026-08-01 10:01:22 ERROR Database connection failed 2026-08-01 10:01:45 WARN Retry connecting to database 2026-08-01 10:02:03 INFO Database connection established 2026-08-01 10:02:30 ERROR Failed to load configuration 2026-08-01 10:03:11 INFO User logout: alice 2026-08-01 10:03:40 ERROR Disk space is low EOF

Command to run

grep "ERROR" input.txt

Output

2026-08-01 10:01:22 ERROR Database connection failed
2026-08-01 10:02:30 ERROR Failed to load configuration
2026-08-01 10:03:40 ERROR Disk space is low

Command to run

grep "ERROR" input.txt | wc -l

Output

3

Command to run

grep "ERROR" input.txt | grep "Database"

Output

2026-08-01 10:01:22 ERROR Database connection failed

Command to run

grep "INFO" input.txt | cut -d' ' -f4-

Output

Application started
User login: alice
Database connection established
User logout: alice

How it works

Command Role
grep "ERROR" input.txt Extracts only the lines containing ERROR
| Passes the output of the previous command to the next (pipe)
wc -l Counts the number of lines passed to it
grep "Database" Further narrows down the result received via the pipe
cut -d' ' -f4- Displays the 4th field onward, split by spaces

Explanation

By combining grep with a pipe (|), you can progressively extract exactly the information you need from a log.
Chaining multiple commands together lets you analyze even large volumes of logs efficiently.

How to improve performance with grep and pipe

Create the file

cat << 'EOF' > input.txt INFO: Server started ERROR: Database connection failed INFO: User login WARNING: Disk usage 85% ERROR: File not found INFO: Backup completed ERROR: Permission denied INFO: Server stopped EOF

Command to run

cat input.txt | grep "ERROR"

Output

ERROR: Database connection failed
ERROR: File not found
ERROR: Permission denied

Command to run

grep "ERROR" input.txt | wc -l

Output

3

Command to run

grep "ERROR" input.txt | sort

Output

ERROR: Database connection failed
ERROR: File not found
ERROR: Permission denied

How it works

Item Description
grep Extracts only the lines containing the specified string
pipe (|) Passes the output of the previous command to the next command
Performance improvement Passing only the needed data to the next command reduces unnecessary processing
Benefit Combining commands enables fast, efficient log analysis and data searches

Explanation

Running grep first to extract only the needed lines, then passing them to later commands, reduces the volume of data to process and improves efficiency.
This is especially effective for log analysis and searching through large amounts of data.

Common errors with grep and pipe and how to fix them

Create the file

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

Command to run

cat input.txt | grep "apple"

Output

apple
apple pie
pineapple

Command to run

cat input.txt | grep "melon"

Output

no output

Command to run

cat input.txt | grep -i "APPLE"

Output

apple
apple pie
pineapple

How it works

Command How it works Common error Fix
cat input.txt | grep "apple" Passes the standard output of cat to grep via a pipe (|), displaying only the matching lines. grep: command not found Check whether grep is installed.
cat input.txt | grep "melon" No output is produced because there is no matching string. Mistaking this for "it's not working" Check the search string and the input data.
cat input.txt | grep -i "APPLE" The -i option searches without distinguishing between uppercase and lowercase. Apple and apple do not match Use the -i option.

Explanation

grep can also search standard input received via a pipe.

When there is no match, this usually ends normally rather than being an error, so it's important to check your search conditions and options.

Summary: practical ways to use grep and pipe

Understanding grep and pipe lets you carry out everyday searches and log analysis more efficiently.

Once you've mastered the basic syntax, combining it with other commands and regular expressions lets you extract the information you need quickly.

Start with the basic usage first, and gradually work your way up to more practical operations.

Leave a Reply

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

©︎ 2025-2026 running terminal commands