Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 24, 2024 07:00
Show Gist options
  • Save copyleftdev/231f3c0af7743b004c0cfe4ef472d877 to your computer and use it in GitHub Desktop.
Save copyleftdev/231f3c0af7743b004c0cfe4ef472d877 to your computer and use it in GitHub Desktop.
The Ultimate Grep Command Guide

πŸ” The Ultimate Grep Command Guide

πŸš€ Basic Usage

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

πŸ“‚ File and Directory Operations

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

πŸ”£ Regular Expressions

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

πŸ”§ Advanced Options

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

🎨 Output Formatting

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

πŸ”— Combining with Other Commands

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

πŸ“Š Practical Examples

  1. Search for IP addresses in a log file:

    grep -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log
  2. Find all files containing a specific string in a directory:

    grep -rnw '/path/to/directory' -e 'pattern'
  3. Search for words starting with 'error' in a case-insensitive manner:

    grep -i '\berror\w*' logfile.txt
  4. 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
  5. Find lines that don't start with a '#' (non-comments):

    grep -v '^#' config.txt
  6. Search for a pattern in gzipped files:

    zgrep 'error' /var/log/syslog.*.gz
  7. Find all PHP files containing 'function' but not 'deprecated':

    grep -l 'function' $(find . -name '*.php') | xargs grep -L 'deprecated'

πŸ† Pro Tips

  1. πŸš€ Use fgrep (or grep -F) for literal string matching (faster than regular grep).
  2. πŸ”£ Learn and use regular expressions to unleash the full power of grep.
  3. 🎭 Use grep -P for Perl-compatible regular expressions (PCRE) in supported versions.
  4. πŸ“š Combine grep with xargs for powerful file operations: grep -l pattern files | xargs command.
  5. πŸ”’ Use grep -i cautiously with security-related searches to avoid case-based oversights.
  6. πŸ“Š For complex patterns, consider using awk or sed in combination with grep.
  7. πŸ” Use grep -f patternfile to search for multiple patterns listed in a file.

πŸ”§ Performance Tips

  1. πŸš„ Use LC_ALL=C grep for faster grep operations (uses byte comparison instead of character comparison).
  2. πŸ“‚ When searching large directories, use find with -exec grep instead of grep -r for better performance.
  3. 🎯 Use anchors (^ and $) when possible to reduce the search space.
  4. πŸ”’ For simple string matching, fgrep (fixed string grep) is faster than standard grep.

πŸ› οΈ Troubleshooting

  • If grep isn't finding a pattern you expect, check for hidden characters using cat -A on your file.
  • For very large files, consider using split to break the file into smaller chunks, then grep those.
  • If grep is slow on a large file, try using grep --mmap if 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! πŸ”πŸ“š

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment