Useful grep
Snippets
Basic Search
- Find a keyword in files:
grep "keyword" filename
- Case-insensitive search:
grep -i "keyword" filename
Recursive Search
- Search recursively in all files:
grep -r "keyword" /path/to/directory
- Search recursively for a pattern while excluding specific directories:
grep -r --exclude-dir="dir_to_exclude" "keyword" /path/to/directory
Pattern Matching
- Search using regular expressions:
grep -E "pattern1|pattern2" filename
- Match whole words only:
grep -w "word" filename
Filter Files
- Find files containing a pattern:
grep -l "keyword" /path/to/directory/*
- Exclude files with certain extensions:
grep --exclude=*.log "keyword" /path/to/files/*
Output Control
- Show line numbers with matches:
grep -n "keyword" filename
- Show only the matched part of the line:
grep -o "pattern" filename
Contextual Search
- Show lines before and after matches:
grep -C 3 "keyword" filename # 3 lines of context
- Show only lines before the match:
grep -B 3 "keyword" filename
- Show only lines after the match:
grep -A 3 "keyword" filename
Advanced Usage
- Search for patterns in compressed files:
zgrep "keyword" file.gz
- Count occurrences of a pattern:
grep -c "keyword" filename
- Invert the match (find lines without the pattern):
grep -v "keyword" filename
Combining with Other Commands
- Pipe output to grep:
ps aux | grep "process_name"
- Highlight matches in terminal output:
grep --color=always "keyword" filename
Debugging & Efficiency
- Display file names along with matches in a directory:
grep -H "keyword" /path/to/directory/*
- Limit search depth in recursive searches:
grep -r --include=*.txt "keyword" --max-depth=2 /path/to/directory
These snippets cover a wide variety of use cases. Let me know if you’d like more examples for specific scenarios!
Extended grep
Snippets
File Type Filtering
- Search only in specific file types:
grep --include=*.{c,h} "keyword" /path/to/directory/*
- Exclude specific file types:
grep --exclude=*.{log,tmp} "keyword" /path/to/directory/*
Binary Files
- Ignore binary files in search:
grep --binary-files=without-match "keyword" /path/to/directory/*
- Search binary files:
grep --binary-files=text "keyword" /path/to/binary_file
Performance Optimizations
- Limit output to first N matches:
grep -m N "keyword" filename
- Search large files without buffering:
grep --line-buffered "keyword" largefile
POSIX Character Classes
- Search for digits:
grep "[[:digit:]]" filename
- Search for alphanumeric characters:
grep "[[:alnum:]]" filename
Custom Delimiters
- Specify output delimiters:
grep -z "keyword" filename # Null-separated output
Multiple Patterns
- Search for multiple keywords (logical OR):
grep -e "keyword1" -e "keyword2" filename
- Search for multiple patterns from a file:
grep -f patterns.txt filename
Miscellaneous
- Print file offsets of matches:
grep -b "keyword" filename
- Print the count of matches per file:
grep -c "keyword" filename
- Suppress error messages:
grep "keyword" filename 2>/dev/null
Multiline Matching
- Search across multiple lines (using Perl-compatible grep):
grep -Pzo "pattern1.*?pattern2" filename
Using grep with xargs
- Find files and search within them:
find /path/to/search -name "*.txt" | xargs grep "keyword"
Combining grep with sed and awk
- Replace pattern in grep results:
grep "pattern" filename | sed 's/pattern/replacement/g'
- Count and format grep results:
grep "pattern" filename | awk '{count++} END {print "Total:", count}'
Environment Variables
- Ignore case permanently by setting environment variable:
export GREP_OPTIONS='--ignore-case'
Highlight Matches in Less
- Search while paging with highlights:
grep "keyword" filename | less -R