Bash Grep

The grep command searches text and prints every line that matches a pattern. It works like the "Find" feature in a document editor, but it scans many files in a blink. System administrators use grep daily to explore log files and code folders.

How Grep Works

  +----------------+      +-------------------+      +----------------+
  |  Input text    | ---> |  grep "pattern"   | ---> |  Matching      |
  |  (file or      |      |  checks each line |      |  lines only    |
  |   pipe)        |      |                   |      |                |
  +----------------+      +-------------------+      +----------------+

Sample File

Create a file named team.txt with these lines:

Asha Tester Pune
Ravi Developer Delhi
Meena Designer Pune
Karan Developer Mumbai
asha Intern Chennai

Basic Search

grep "Pune" team.txt

Output:

Asha Tester Pune
Meena Designer Pune

Common Options

OptionAction
-iIgnore uppercase and lowercase
-vShow lines that do not match
-nShow line numbers
-cCount matching lines
-wMatch whole words only
-rSearch folders recursively
-lList only the file names
-A, -B, -CShow lines after, before, or around a match
-EUse extended regular expressions

Examples for Each Option

Ignore Case

grep -i "asha" team.txt

The command prints both "Asha Tester Pune" and "asha Intern Chennai".

Invert the Match

grep -v "Developer" team.txt

The output skips every Developer line.

Show Line Numbers

grep -n "Pune" team.txt
1:Asha Tester Pune
3:Meena Designer Pune

Count Matches

grep -c "Developer" team.txt

The screen shows 2.

Search Many Files

Search all text files in the current folder:

grep "error" *.log

Search every file inside a folder and its subfolders:

grep -rn "TODO" project/

Each result starts with the file name and line number, which makes jumping to the spot easy.

Show Context Around a Match

grep -C 2 "failed" app.log

This command prints two lines before and two lines after each match. Context often explains the cause of an error.

Use Patterns

Grep understands special symbols that stand for groups of characters.

grep "^Asha" team.txt      # lines that start with Asha
grep "Pune$" team.txt      # lines that end with Pune
grep "R.vi" team.txt       # dot matches any single character
grep "[AK]" team.txt       # lines with A or K

Extended Patterns With -E

grep -E "Pune|Delhi" team.txt
grep -E "^[A-Z][a-z]+ Developer" team.txt

The pipe inside -E means "or". The second line finds names that start with a capital letter and are followed by the word Developer.

Combine Grep With Pipes

Grep filters the output of other commands:

ps aux | grep "bash"
history | grep "chmod"
ls -l | grep "^d"

The last line lists only folders because folder lines begin with the letter d.

Use Grep Inside a Script

Grep returns an exit status. The value 0 means a match exists. The value 1 means no match exists. The quiet option -q hides the output and keeps only the status.

#!/bin/bash
if grep -q "Karan" team.txt; then
  echo "Karan is on the team"
else
  echo "Karan not found"
fi

Practical Example: Count Errors in a Log

#!/bin/bash
total=$(grep -ci "error" server.log)
echo "Errors found: $total"

Key Takeaways

  • grep pattern file prints matching lines.
  • Use -i for case-free search and -v to reverse the match.
  • -r searches whole folders and -n adds line numbers.
  • -E enables flexible patterns with | and +.
  • -q suits scripts because it returns only a status.