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 ChennaiBasic Search
grep "Pune" team.txtOutput:
Asha Tester Pune
Meena Designer PuneCommon Options
| Option | Action |
|---|---|
| -i | Ignore uppercase and lowercase |
| -v | Show lines that do not match |
| -n | Show line numbers |
| -c | Count matching lines |
| -w | Match whole words only |
| -r | Search folders recursively |
| -l | List only the file names |
| -A, -B, -C | Show lines after, before, or around a match |
| -E | Use extended regular expressions |
Examples for Each Option
Ignore Case
grep -i "asha" team.txtThe command prints both "Asha Tester Pune" and "asha Intern Chennai".
Invert the Match
grep -v "Developer" team.txtThe output skips every Developer line.
Show Line Numbers
grep -n "Pune" team.txt
1:Asha Tester Pune
3:Meena Designer PuneCount Matches
grep -c "Developer" team.txtThe screen shows 2.
Search Many Files
Search all text files in the current folder:
grep "error" *.logSearch 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.logThis 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 KExtended Patterns With -E
grep -E "Pune|Delhi" team.txt
grep -E "^[A-Z][a-z]+ Developer" team.txtThe 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"
fiPractical Example: Count Errors in a Log
#!/bin/bash
total=$(grep -ci "error" server.log)
echo "Errors found: $total"Key Takeaways
grep pattern fileprints matching lines.- Use
-ifor case-free search and-vto reverse the match. -rsearches whole folders and-nadds line numbers.-Eenables flexible patterns with|and+.-qsuits scripts because it returns only a status.
