You are currently viewing Grep Command Cheat Sheet

Grep Command Cheat Sheet

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

#CommandDescription
1.1grep "pattern" filenameSearch for a specific pattern in a file.
1.2grep "pattern" file1 file2Search for a pattern in multiple files.
1.3grep -r "pattern" directorySearch recursively for a pattern in directories.
2.1grep -i "pattern" filenamePerform a case-insensitive search.
2.2grep -n "pattern" filenameDisplay line numbers for matching lines.
2.3grep -o "pattern" filenameDisplay only the matching part of lines.
2.4grep -v "pattern" filenameInvert match, exclude lines containing the pattern.
2.5grep -c "pattern" filenameDisplay the count of matching lines.
3.1grep "^pattern" filenameBasic Regular Expression (BRE) – Match at the beginning of a line.
3.2grep -E "pattern" filenameExtended Regular Expression (ERE).
3.3grep "p.ttern" filenameMatch any single character.
3.4grep "pat*ern" filenameMatch zero or more occurrences of the preceding element.
3.5grep "^pattern" filename / grep "pattern$" filenameMatch at the beginning/end of a line.
4.1grep -B 2 "pattern" filenameDisplay 2 lines before the match.
4.2grep -A 3 "pattern" filenameDisplay 3 lines after the match.
4.3grep -C 2 "pattern" filenameDisplay 2 lines before and after the match.
5.1grep "pattern" *.txtSearch for a pattern in specific file types.
6.1command | grep "pattern"Use Grep with other commands.
6.2command | grep -c "pattern"Count the number of lines matching a pattern.
7.1tail -f logfile | grep "pattern"Monitor log files in real-time.
8.1grep --helpDisplay Grep help.
8.2grep --versionDisplay Grep version.
9.1grep --color=auto "pattern" filenameHighlight matching text.
10.1grep -e "pattern1" -e "pattern2" filenameSearch for lines matching any of the patterns.
10.2grep "pattern1" filename | grep "pattern2"Search for lines matching both patterns.
11.1grep -A 5 "pattern" filenameDisplay 5 lines after the match.
11.2grep -B 3 "pattern" filenameDisplay 3 lines before the match.
12.1grep -w "pattern" filenameMatch whole words only.
13.1grep -l "pattern" directory/*List files containing the pattern.
14.1grep -a "pattern" binaryfileSearch in binary files.
14.2grep -I "pattern" *Exclude binary files from search.
15.1grep -P "pattern" filenameUse PCRE for advanced pattern matching.
16.1grep -q "pattern" filenameSuppress error messages.
17.1grep -n -B 1 -A 5 "^function" sourcecode.cDisplay function name and lines.
18.1grep -r --exclude-dir=dir_to_exclude "pattern" directorySearch recursively, exclude certain directories.
19.1grep -n "pattern" filenameDisplay line numbers with matching lines.
19.2grep -n "pattern" filename | cut -d: -f1Display only line numbers.
20.1grep -o "pattern" filename | wc -lCount the occurrences of a pattern.
21.1grep -P -o "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" filenameMatch IP addresses.
22.1grep -P -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z | a-z]{2,}\b" filenameMatch email addresses.
23.1grep -e "pattern1" -e "pattern2" filenameSearch for lines containing either pattern1 or pattern2.
23.2grep "pattern1" filename | grep "pattern2"Search for lines containing both pattern1 and pattern2.
24.1grep -P "(?=.*pattern1)(?=.*pattern2)" filenameMatch lines with positive lookahead.
25.1grep -P "(\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b)" filenameMatch numeric ranges.
26.1grep -x "pattern" filenameMatch whole lines containing the pattern.
27.1grep --color=always -C 2 "pattern" filenameHighlight matching text with context.

Leave a Reply