Useful grep Snippets


  1. Find a keyword in files:
    grep "keyword" filename
    
  2. Case-insensitive search:
    grep -i "keyword" filename
    

  1. Search recursively in all files:
    grep -r "keyword" /path/to/directory
    
  2. Search recursively for a pattern while excluding specific directories:
    grep -r --exclude-dir="dir_to_exclude" "keyword" /path/to/directory
    

Pattern Matching

  1. Search using regular expressions:
    grep -E "pattern1|pattern2" filename
    
  2. Match whole words only:
    grep -w "word" filename
    

Filter Files

  1. Find files containing a pattern:
    grep -l "keyword" /path/to/directory/*
    
  2. Exclude files with certain extensions:
    grep --exclude=*.log "keyword" /path/to/files/*
    

Output Control

  1. Show line numbers with matches:
    grep -n "keyword" filename
    
  2. Show only the matched part of the line:
    grep -o "pattern" filename
    

  1. Show lines before and after matches:
    grep -C 3 "keyword" filename  # 3 lines of context
    
  2. Show only lines before the match:
    grep -B 3 "keyword" filename
    
  3. Show only lines after the match:
    grep -A 3 "keyword" filename
    

Advanced Usage

  1. Search for patterns in compressed files:
    zgrep "keyword" file.gz
    
  2. Count occurrences of a pattern:
    grep -c "keyword" filename
    
  3. Invert the match (find lines without the pattern):
    grep -v "keyword" filename
    

Combining with Other Commands

  1. Pipe output to grep:
    ps aux | grep "process_name"
    
  2. Highlight matches in terminal output:
    grep --color=always "keyword" filename
    

Debugging & Efficiency

  1. Display file names along with matches in a directory:
    grep -H "keyword" /path/to/directory/*
    
  2. 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

  1. Search only in specific file types:
    grep --include=*.{c,h} "keyword" /path/to/directory/*
    
  2. Exclude specific file types:
    grep --exclude=*.{log,tmp} "keyword" /path/to/directory/*
    

Binary Files

  1. Ignore binary files in search:
    grep --binary-files=without-match "keyword" /path/to/directory/*
    
  2. Search binary files:
    grep --binary-files=text "keyword" /path/to/binary_file
    

Performance Optimizations

  1. Limit output to first N matches:
    grep -m N "keyword" filename
    
  2. Search large files without buffering:
    grep --line-buffered "keyword" largefile
    

POSIX Character Classes

  1. Search for digits:
    grep "[[:digit:]]" filename
    
  2. Search for alphanumeric characters:
    grep "[[:alnum:]]" filename
    

Custom Delimiters

  1. Specify output delimiters:
    grep -z "keyword" filename  # Null-separated output
    

Multiple Patterns

  1. Search for multiple keywords (logical OR):
    grep -e "keyword1" -e "keyword2" filename
    
  2. Search for multiple patterns from a file:
    grep -f patterns.txt filename
    

Miscellaneous

  1. Print file offsets of matches:
    grep -b "keyword" filename
    
  2. Print the count of matches per file:
    grep -c "keyword" filename
    
  3. Suppress error messages:
    grep "keyword" filename 2>/dev/null
    

Multiline Matching

  1. Search across multiple lines (using Perl-compatible grep):
    grep -Pzo "pattern1.*?pattern2" filename
    

Using grep with xargs

  1. Find files and search within them:
    find /path/to/search -name "*.txt" | xargs grep "keyword"
    

Combining grep with sed and awk

  1. Replace pattern in grep results:
    grep "pattern" filename | sed 's/pattern/replacement/g'
    
  2. Count and format grep results:
    grep "pattern" filename | awk '{count++} END {print "Total:", count}'
    

Environment Variables

  1. Ignore case permanently by setting environment variable:
    export GREP_OPTIONS='--ignore-case'
    

Highlight Matches in Less

  1. Search while paging with highlights:
    grep "keyword" filename | less -R