1. Basic Grep Commands:
1.1 Search for a Pattern in a File
grep "pattern" filename
1.2 Search for a Pattern in Multiple Files
grep "pattern" filename
1.3 Search Recursively in Directories
grep -r "pattern" filename
2. Grep Options:
2.1 Case-Insensitive Search
grep -i "pattern" filename
2.2 Display Line Numbers
grep -n "pattern" filename
2.3 Display Only Matching Part of Lines
grep -o "pattern" filename
2.4 Invert Match (Exclude Lines Containing the Pattern)
grep -v "pattern" filename
2.5 Display Count of Matching Lines
grep -c "pattern" filename
3. Grep with Regular Expressions:
3.1 Basic Regular Expression (BRE)
grep "^pattern" filename
3.2 Extended Regular Expression (ERE)
grep -E "pattern" filename
3.3 Match Any Single Character
grep "p.ttern" filename
3.4 Match Zero or More Occurrences
grep "pat*ern" filename
3.5 Match Beginning/End of Line
grep "^pattern" filename
grep "pattern$" filename
4. Grep with Context:
4.1 Display Lines Before/After Match
grep -B 2 "pattern" filename # Display 2 lines before
grep -A 3 "pattern" filename # Display 3 lines after
grep -C 2 "pattern" filename # Display 2 lines before and after
5. Grep with File Types:
5.1 Search for a Pattern in Specific File Types
grep "pattern" *.txt
grep "pattern" *.log
6. Grep with Pipelines:
6.1 Use Grep with Other Commands
command | grep "pattern"
6.2 Count the Number of Lines Matching a Pattern
command | grep -c "pattern"
7. Grep in Real-Time:
7.1 Monitor Log Files in Real-Time
tail -f logfile | grep "pattern"
8. Grep Help:
8.1 Display Grep Help
grep --help
8.2 Display Grep Version
grep --version
9. Grep with Color Highlighting:
9.1 Highlight Matching Text
grep --color=auto "pattern" filename
10. Grep with Multiple Patterns:
10.1 Search for Lines Matching Any of the Patterns
grep -e "pattern1" -e "pattern2" filename
10.2 Search for Lines Matching All Patterns
grep "pattern1" filename | grep "pattern2"
11. Grep with Line Limiting:
11.1 Display N Lines After Match
grep -A 5 "pattern" filename # Display 5 lines after
11.2 Display N Lines Before Match
grep -B 3 "pattern" filename # Display 3 lines before
12. Grep with Word Match:
12.1 Match Whole Words Only
grep -w "pattern" filename
13. Grep for Files Containing a Pattern:
13.1 List Files Containing the Pattern
grep -l "pattern" directory/*
14. Grep with Binary Files:
14.1 Search in Binary Files
grep -a "pattern" binaryfile
14.2 Exclude Binary Files from Search
grep -I "pattern" *
15. Grep with Perl-Compatible Regular Expressions (PCRE):
15.1 Use PCRE for Advanced Pattern Matching
grep -P "pattern" filename
16. Grep with Quiet Mode:
16.1 Suppress Error Messages
grep -q "pattern" filename
17. Grep with Context of Function Names:
17.1 Display Function Name and Lines
grep -n -B 1 -A 5 "^function" sourcecode.c
18. Grep with Recursive Exclusion:
18.1 Search Recursively, Exclude Certain Directories
grep -r --exclude-dir=dir_to_exclude "pattern" directory
19. Grep with Context of Line Numbers:
19.1 Display Line Numbers with Matching Lines
grep -n "pattern" filename
19.2 Display Only Line Numbers
grep -n "pattern" filename | cut -d: -f1
20. Grep with Word Count:
20.1 Count the Occurrences of a Pattern
grep -o "pattern" filename | wc -l
21. Grep for IP Addresses:
21.1 Match IP Addresses
grep -P -o "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" filename
22. Grep for Email Addresses:
22.1 Match Email Addresses
grep -P -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" filename
23. Grep with Lookahead and Lookbehind:
23.1 Match Lines with Positive Lookahead
grep -P "(?=.*pattern1)(?=.*pattern2)" filename
24. Grep for Numeric Ranges:
24.1 Match Numeric Ranges
grep -P "(\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b)" filename
25. Grep for Whole Lines:
25.1 Match Whole Lines Containing the Pattern
grep -x "pattern" filename
26. Grep with Highlighted Context:
26.1 Highlight Matching Text with Context
grep --color=always -C 2 "pattern" filename
Table Of Commands
# | Command | Description | |
---|---|---|---|
1.1 | grep "pattern" filename | Search for a specific pattern in a file. | |
1.2 | grep "pattern" file1 file2 | Search for a pattern in multiple files. | |
1.3 | grep -r "pattern" directory | Search recursively for a pattern in directories. | |
2.1 | grep -i "pattern" filename | Perform a case-insensitive search. | |
2.2 | grep -n "pattern" filename | Display line numbers for matching lines. | |
2.3 | grep -o "pattern" filename | Display only the matching part of lines. | |
2.4 | grep -v "pattern" filename | Invert match, exclude lines containing the pattern. | |
2.5 | grep -c "pattern" filename | Display the count of matching lines. | |
3.1 | grep "^pattern" filename | Basic Regular Expression (BRE) – Match at the beginning of a line. | |
3.2 | grep -E "pattern" filename | Extended Regular Expression (ERE). | |
3.3 | grep "p.ttern" filename | Match any single character. | |
3.4 | grep "pat*ern" filename | Match zero or more occurrences of the preceding element. | |
3.5 | grep "^pattern" filename / grep "pattern$" filename | Match at the beginning/end of a line. | |
4.1 | grep -B 2 "pattern" filename | Display 2 lines before the match. | |
4.2 | grep -A 3 "pattern" filename | Display 3 lines after the match. | |
4.3 | grep -C 2 "pattern" filename | Display 2 lines before and after the match. | |
5.1 | grep "pattern" *.txt | Search for a pattern in specific file types. | |
6.1 | command | grep "pattern" | Use Grep with other commands. | |
6.2 | command | grep -c "pattern" | Count the number of lines matching a pattern. | |
7.1 | tail -f logfile | grep "pattern" | Monitor log files in real-time. | |
8.1 | grep --help | Display Grep help. | |
8.2 | grep --version | Display Grep version. | |
9.1 | grep --color=auto "pattern" filename | Highlight matching text. | |
10.1 | grep -e "pattern1" -e "pattern2" filename | Search for lines matching any of the patterns. | |
10.2 | grep "pattern1" filename | grep "pattern2" | Search for lines matching both patterns. | |
11.1 | grep -A 5 "pattern" filename | Display 5 lines after the match. | |
11.2 | grep -B 3 "pattern" filename | Display 3 lines before the match. | |
12.1 | grep -w "pattern" filename | Match whole words only. | |
13.1 | grep -l "pattern" directory/* | List files containing the pattern. | |
14.1 | grep -a "pattern" binaryfile | Search in binary files. | |
14.2 | grep -I "pattern" * | Exclude binary files from search. | |
15.1 | grep -P "pattern" filename | Use PCRE for advanced pattern matching. | |
16.1 | grep -q "pattern" filename | Suppress error messages. | |
17.1 | grep -n -B 1 -A 5 "^function" sourcecode.c | Display function name and lines. | |
18.1 | grep -r --exclude-dir=dir_to_exclude "pattern" directory | Search recursively, exclude certain directories. | |
19.1 | grep -n "pattern" filename | Display line numbers with matching lines. | |
19.2 | grep -n "pattern" filename | cut -d: -f1 | Display only line numbers. | |
20.1 | grep -o "pattern" filename | wc -l | Count the occurrences of a pattern. | |
21.1 | grep -P -o "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" filename | Match IP addresses. | |
22.1 | grep -P -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z | a-z]{2,}\b" filename | Match email addresses. | |
23.1 | grep -e "pattern1" -e "pattern2" filename | Search for lines containing either pattern1 or pattern2. | |
23.2 | grep "pattern1" filename | grep "pattern2" | Search for lines containing both pattern1 and pattern2. | |
24.1 | grep -P "(?=.*pattern1)(?=.*pattern2)" filename | Match lines with positive lookahead. | |
25.1 | grep -P "(\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b)" filename | Match numeric ranges. | |
26.1 | grep -x "pattern" filename | Match whole lines containing the pattern. | |
27.1 | grep --color=always -C 2 "pattern" filename | Highlight matching text with context. |