| Command | Description | Example |
|---|---|---|
grep pattern file |
π Search for a pattern in a file | grep error logfile.txt |
grep -i pattern file |
π Case-insensitive search | grep -i ERROR logfile.txt |
grep -w word file |
π€ Match whole words only | grep -w error logfile.txt |
grep -v pattern file |
β Invert match (show lines that don't match) | grep -v success logfile.txt |
| Command | Description | Example |
|---|---|---|
grep -r pattern directory |
ποΈ Recursive search in directory | grep -r password /etc/ |
grep -l pattern files |
π List files that match pattern | grep -l error *.log |
grep -c pattern file |
π’ Count occurrences of pattern | grep -c error logfile.txt |
| Command | Description | Example |
|---|---|---|
grep '^pattern' file |
π¬ Match pattern at start of line | grep '^ERROR' logfile.txt |
grep 'pattern$' file |
π Match pattern at end of line | grep 'FAILED$' logfile.txt |
grep '[0-9]' file |
π’ Match any digit | grep '[0-9]' data.txt |
grep 'error|warning' file |
π Match multiple patterns (OR) | grep 'error|warning' logfile.txt |
| Option | Description | Example |
|---|---|---|
-A n |
π½ Show n lines after match | grep -A 3 error logfile.txt |
-B n |
πΌ Show n lines before match | grep -B 2 error logfile.txt |
-C n |
πΌπ½ Show n lines before and after match | grep -C 1 error logfile.txt |
-n |
π’ Show line numbers | grep -n error logfile.txt |
-E |
π£ Use extended regular expressions | grep -E 'error|warning' logfile.txt |
| Option | Description | Example |
|---|---|---|
--color |
π Highlight matching text | grep --color error logfile.txt |
-o |
π‘ Show only the matched parts of a line | grep -o 'ERROR.*' logfile.txt |
| Command | Description | Example |
|---|---|---|
command | grep pattern |
πΏ Pipe output of a command to grep | ls -l | grep '^d' |
grep pattern file | wc -l |
π’ Count matching lines | grep error logfile.txt | wc -l |
-
Search for IP addresses in a log file:
grep -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log -
Find all files containing a specific string in a directory:
grep -rnw '/path/to/directory' -e 'pattern'
-
Search for words starting with 'error' in a case-insensitive manner:
grep -i '\berror\w*' logfile.txt -
Extract email addresses from a file:
grep -E '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' file.txt -
Find lines that don't start with a '#' (non-comments):
grep -v '^#' config.txt -
Search for a pattern in gzipped files:
zgrep 'error' /var/log/syslog.*.gz
-
Find all PHP files containing 'function' but not 'deprecated':
grep -l 'function' $(find . -name '*.php') | xargs grep -L 'deprecated'
- π Use
fgrep(orgrep -F) for literal string matching (faster than regular grep). - π£ Learn and use regular expressions to unleash the full power of grep.
- π Use
grep -Pfor Perl-compatible regular expressions (PCRE) in supported versions. - π Combine grep with
xargsfor powerful file operations:grep -l pattern files | xargs command. - π Use
grep -icautiously with security-related searches to avoid case-based oversights. - π For complex patterns, consider using
awkorsedin combination with grep. - π Use
grep -f patternfileto search for multiple patterns listed in a file.
- π Use
LC_ALL=C grepfor faster grep operations (uses byte comparison instead of character comparison). - π When searching large directories, use
findwith-exec grepinstead ofgrep -rfor better performance. - π― Use anchors (^ and $) when possible to reduce the search space.
- π’ For simple string matching,
fgrep(fixed string grep) is faster than standard grep.
- If grep isn't finding a pattern you expect, check for hidden characters using
cat -Aon your file. - For very large files, consider using
splitto break the file into smaller chunks, then grep those. - If grep is slow on a large file, try using
grep --mmapif available on your system.
Remember, grep is an incredibly powerful tool, but with great power comes great responsibility. Always double-check your patterns, especially when using grep with system files or in scripts. Happy grepping! ππ