Introduction
grep is a representative command for performing string searches in Linux and Unix environments.
When analyzing logs or checking configuration files, there are often situations where you want to search while excluding unnecessary lines.
However, beginners tend to stumble over how to use options and regular expressions.
This article explains the basics through practical applications of excluding with grep in an easy-to-understand way.
Reference: GNU grep
Basic syntax and usage for excluding with grep
Creating the file
cat << 'EOF' > input.txt
apple
banana
orange
grape
apple pie
banana split
EOF
Command to run
grep -v "banana" input.txt
Result
apple
orange
grape
apple pie
Command to run
grep -v "apple" input.txt
Result
banana
orange
grape
banana split
Command to run
grep -Ev "apple|banana" input.txt
Result
orange
grape
How it works
| Syntax | Description |
|---|---|
| grep "string" file | Displays lines containing the specified string |
| grep -v "string" file | Displays lines excluding the specified string |
| grep -E "A|B" file | Multiple conditions (OR search) |
| grep -Ev "A|B" file | Excludes lines matching multiple conditions |
| grep -vi "string" file | Excludes without distinguishing uppercase/lowercase |
Explanation
Excluding with grep can be achieved by using the -v option.
Combining it with -E allows you to exclude multiple patterns at once, which is often used in log analysis and text extraction.
The meaning of the -v option for exclusion searches with grep
Creating the file
cat << 'EOF' > input.txt
apple
orange
banana
error
grape
error_log
melon
EOF
Command to run
grep -v "error" input.txt
Result
apple
orange
banana
grape
melon
Command to run
grep -v "banana" input.txt
Result
apple
orange
error
grape
error_log
melon
Command to run
grep -v -e "error" -e "banana" input.txt
Result
apple
orange
grape
melon
How it works
| Item | Content |
|---|---|
| grep | Command that searches for strings within a file |
| -v | Excludes and displays lines that match the condition |
| Normal search | Displays only matching lines |
| Exclusion search | Displays everything except matching lines |
| Multiple exclusions | Multiple conditions can be excluded by combining -e |
Explanation
grep -v is an option that excludes lines matching the search condition from the results.
It is often used for excluding with grep, such as when you want to exclude unnecessary messages from a log file.
How to exclude specific strings with grep
Creating the file
cat << 'EOF' > input.txt
apple
banana
orange
apple pie
grape
EOF
Command to run
grep -v "apple" input.txt
Result
banana
orange
grape
Command to run
grep -Ev "apple|orange" input.txt
Result
banana
grape
Command to run
grep -Fv "apple pie" input.txt
Result
apple
banana
orange
grape
How it works
| Option | Description | Example |
|---|---|---|
| -v | Excludes and displays matching lines | grep -v "apple" input.txt |
| -E | Uses extended regular expressions | grep -Ev "apple|orange" input.txt |
| -F | Searches as a fixed string rather than a regular expression | grep -Fv "apple pie" input.txt |
| apple|orange | Excludes lines matching apple or orange | grep -Ev "apple|orange" input.txt |
Explanation
Use the -v option when excluding with grep.
When you want to exclude multiple strings, combining -E with | lets you do this efficiently.
How to exclude multiple keywords with grep
Creating the file
cat << 'EOF' > input.txt
grep
grep exclude
awk
sed
grep exclude pattern
find
EOF
Command to run
grep -v -E 'grep|exclude' input.txt
Result
awk
sed
find
Command to run
grep -v -e 'grep' -e 'exclude' input.txt
Result
awk
sed
find
How it works
| Item | Content |
|---|---|
| grep -v | Excludes and displays matching lines |
| -E | Uses extended regular expressions |
| grep|exclude | Matches lines containing grep or exclude |
| -e | Allows you to specify multiple keywords to exclude |
| Processing result | Excludes lines containing the specified keywords from the output |
Explanation
Using grep -v, you can exclude lines that match a specified keyword.
When excluding multiple keywords, using the OR condition (|) with -E, or specifying -e multiple times, is convenient.
How to exclude using regular expressions with grep
Creating the file
cat << 'EOF' > input.txt
apple
banana
orange
grape
pineapple
EOF
Command to run
grep -v 'banana' input.txt
Result
apple
orange
grape
pineapple
Command to run
grep -Ev 'apple|orange' input.txt
Result
banana
grape
Command to run
grep -Ev 'apple$' input.txt
Result
banana
orange
grape
How it works
| Option/Regular expression | Description |
|---|---|
| -v | Excludes and displays matching lines |
| -E | Enables extended regular expressions |
| apple|orange | Matches apple or orange |
| apple$ | Matches lines ending with apple |
| ^apple | Matches lines starting with apple |
Explanation
Use the -v option to exclude with grep.
Combined with regular expressions, flexible exclusion searches are possible, such as multiple conditions or specifying the beginning/end of a line.
How to exclude ignoring uppercase and lowercase with grep
Creating the file
cat << 'EOF' > input.txt
Linux
linux
LINUX
Ubuntu
CentOS
ubuntu
EOF
Command to run
grep -iv "linux" input.txt
Result
Ubuntu
CentOS
ubuntu
Command to run
grep -iv "ubuntu" input.txt
Result
Linux
linux
LINUX
CentOS
How it works
| Option | Description |
|---|---|
| grep | Command that searches text |
| -i | Searches without distinguishing uppercase and lowercase |
| -v | Excludes and displays matching lines |
| grep -iv "linux" input.txt | Excludes lines matching linux (Linux, linux, LINUX, etc.) |
Explanation
Excluding with grep uses the -v option.
Combining it further with -i lets you exclude matching lines while ignoring uppercase and lowercase.
How to exclude blank lines with grep
Creating the file
cat << 'EOF' > input.txt
aaa
bbb
ccc
EOF
Command to run
grep -v '^$' input.txt
Result
aaa
bbb
ccc
How it works
| Item | Content |
|---|---|
| grep | Command that performs text searches |
| -v | Excludes matching lines (excluding with grep) |
| ^$ | Regular expression representing a blank line (no characters from start to end of line) |
| Processing content | Excludes lines matching blank lines and displays the rest |
Explanation
grep -v '^$' excludes lines matching blank lines and outputs the rest.
It is often used when you want to remove unnecessary blank lines from logs or configuration files.
How to exclude comment lines with grep
Creating the file
cat << 'EOF' > input.txt
# comment
apple
# comment to exclude
banana
orange
# memo
grape
EOF
Command to run
grep -v '^#' input.txt
Result
apple
banana
orange
grape
Command to run
grep -v '^#' input.txt | grep 'an'
Result
banana
orange
How it works
| Item | Content |
|---|---|
| grep | Command that searches for strings within a file |
| -v | Option that excludes and displays matching lines |
| ^# | Regular expression representing a comment line starting with # at the beginning of the line (^) |
| grep -v '^#' | Excludes comment lines and displays the rest |
| | | Pipe that passes the output of the previous command to the next command |
Explanation
Using grep -v, you can easily exclude lines matching a specified pattern.
By excluding comment lines before searching, you can check configuration files and logs more efficiently.
How to exclude only exactly matching lines with grep
Creating the file
cat << 'EOF' > input.txt
apple
orange
banana
orange juice
orange
grape
EOF
Command to run
grep -vx 'orange' input.txt
Result
apple
banana
orange juice
grape
How it works
| Option | Description |
|---|---|
| -v | Excludes and displays matching lines |
| -x | Matches only when the entire line matches exactly |
| grep -vx 'orange' input.txt | Excludes only lines that exactly match orange |
| orange juice | Not excluded since it is not an exact match |
Explanation
With grep -v alone, partially matching lines are also subject to exclusion.
By combining it with -x, you can exclude only lines that exactly match the specified string.
How to exclude by combining multiple conditions with grep
Creating the file
cat << 'EOF' > input.txt
INFO Start
ERROR Connection failed
WARN Disk usage high
DEBUG Test message
ERROR Database error
INFO End
EOF
Command to run
grep -Ev 'ERROR|DEBUG' input.txt
Result
INFO Start
WARN Disk usage high
INFO End
Command to run
grep -Ev 'ERROR|WARN' input.txt
Result
INFO Start
DEBUG Test message
INFO End
Command to run
grep -v -e 'ERROR' -e 'DEBUG' input.txt
Result
INFO Start
WARN Disk usage high
INFO End
How it works
| Option | Description |
|---|---|
| -v | Excludes and displays matching lines |
| -E | Enables extended regular expressions |
| ERROR|DEBUG | Matches ERROR or DEBUG |
| -e | Specifies additional search conditions |
| grep -Ev 'ERROR|DEBUG' | Excludes lines matching multiple conditions at once |
| grep -v -e 'ERROR' -e 'DEBUG' | Excludes by specifying conditions individually |
Explanation
Excluding with grep uses -v.
When specifying multiple conditions, the method of using -E with | or specifying -e multiple times is often used.
How to specify exclusion targets on a word-by-word basis with grep
Creating the file
cat << 'EOF' > input.txt
apple
orange
grape
pineapple
apple juice
orange juice
EOF
Command to run
grep -vw 'apple' input.txt
Result
orange
grape
pineapple
orange juice
Command to run
grep -vw 'orange' input.txt
Result
apple
grape
pineapple
apple juice
Command to run
grep -vwE 'apple|orange' input.txt
Result
grape
pineapple
How it works
| Option | Content |
|---|---|
| -v | Excludes matching lines (excluding with grep) |
| -w | Matches on a word-by-word basis |
| -E | Uses extended regular expressions |
| apple | Matches apple as a word |
| orange | Matches orange as a word |
| apple|orange | Matches apple or orange |
Explanation
grep -v excludes matching lines.
When used together with -w, matching is determined on a word-by-word basis, so lines containing apple juice or orange juice as a word are also subject to matching and excluded.
On the other hand, pineapple is treated as a different word, so it is not excluded.
How to exclude specific files when searching with grep
Creating the file
cat << 'EOF' > input.txt
error: database connection failed
info: process started
error: timeout occurred
warning: disk usage high
EOF
Creating the file
cat << 'EOF' > exclude.txt
error: database connection failed
EOF
Command to run
grep "error" input.txt
Result
error: database connection failed
error: timeout occurred
Command to run
grep "error" input.txt | grep -v -f exclude.txt
Result
error: timeout occurred
Command to run
grep --exclude="exclude.txt" "error" *.txt
Result
input.txt:error: database connection failed
input.txt:error: timeout occurred
How it works
| Command | How it works |
|---|---|
| grep "error" input.txt | Searches input.txt for lines containing error |
| grep -v -f exclude.txt | Excludes lines matching the patterns listed in exclude.txt |
| grep --exclude="exclude.txt" "error" *.txt | Excludes exclude.txt itself from the files being searched |
| -v | Excludes matching lines (inverted search) |
| --exclude | Excludes the specified file name from the search target |
Explanation
There are two ways to exclude with grep: "excluding a specific pattern from the search results (-v)" and "excluding the search target file itself (--exclude)."
By using each appropriately depending on the use case, you can search efficiently.
How to exclude a specific directory with grep
Creating the file
cat << 'EOF' > input.txt
sample text
error message
warning message
debug log
error detected
EOF
Creating the file
mkdir -p logs/archive/
Creating the file
cp input.txt logs/app.log
Creating the file
cp input.txt logs/archive/old.log
Command to run
grep -r --exclude-dir=archive "error" logs
Result
logs/app.log:error message
logs/app.log:error detected
How it works
| Item | Content |
|---|---|
| grep -r | Searches a directory recursively |
| --exclude-dir=archive | Excludes the archive directory from the search target |
| "error" | The string to search for |
| logs | The directory to start the search from |
Explanation
Using grep's --exclude-dir option, you can search while excluding unnecessary directories.
This is convenient when you want to exclude log storage directories or backup directories from the target.
How to combine exclusion search with the find command in grep
Creating the file
cat << 'EOF' > input.txt
error: database connection failed
info: application started
warning: disk usage high
error: timeout occurred
info: backup completed
EOF
Command to run
find . -name "input.txt" -exec grep -v "error" {} \;
Result
info: application started
warning: disk usage high
info: backup completed
Command to run
find . -name "*.txt" -exec grep -H -v "error" {} \;
Result
./input.txt:info: application started
./input.txt:warning: disk usage high
./input.txt:info: backup completed
How it works
| Command element | Role |
|---|---|
| find . | Searches below the current directory |
| -name "input.txt" | Specifies the target file name |
| -name "*.txt" | Specifies files with the txt extension |
| -exec | Executes a command on the found files |
| grep -v "error" | Excludes lines containing error and displays the rest (excluding with grep) |
| {} | Replaced with the file name found by find |
| \; | Indicates the end of -exec |
Explanation
Using grep -v, you can exclude lines containing a specified string.
By combining it with find, you can efficiently run exclusion searches across multiple files.
Causes and solutions when exclusion search doesn’t work with grep
Creating the file
cat << 'EOF' > input.txt
INFO: start
DEBUG: initialize
ERROR: connection failed
INFO: retry
DEBUG: retry process
ERROR: timeout
INFO: end
EOF
Command to run
grep -v "DEBUG" input.txt
Result
INFO: start
ERROR: connection failed
INFO: retry
ERROR: timeout
INFO: end
Command to run
grep -v DEBUG input.txt
Result
INFO: start
ERROR: connection failed
INFO: retry
ERROR: timeout
INFO: end
Command to run
grep -v "DEBUG\|ERROR" input.txt
Result
INFO: start
INFO: retry
INFO: end
Command to run
grep -Ev "DEBUG|ERROR" input.txt
Result
INFO: start
INFO: retry
INFO: end
How it works
| Cause | Content | Solution |
|---|---|---|
| Mistake in specifying multiple conditions | grep -v "DEBUG|ERROR" | Use grep -Ev "DEBUG|ERROR" or grep -v "DEBUG\|ERROR" |
| Error in regular expression | | is not treated as a special character | Add -E or use \| |
| Difference in uppercase/lowercase | debug and DEBUG are different strings | Use grep -iv "debug" |
| Insufficient confirmation of exclusion target | A different string than expected is included | First check the matching content with grep "target string" |
Explanation
Exclusion searches with grep can be achieved with the -v option, but when specifying multiple conditions, the way regular expressions are handled often causes it to not work as expected.
By using -E and checking patterns, you can solve many of these problems.
Understanding exclusion with grep for efficient searching
The exclusion feature of grep is an essential function for quickly extracting only the information you need.
By understanding the -v option along with regular expressions and how to specify multiple conditions, you can greatly improve search accuracy.
Additionally, by making use of excluding blank lines and comment lines, as well as excluding specific files and directories, you can efficiently find the information you need from large amounts of data.
Master excluding with grep correctly and make your daily work more comfortable.

