Cheat Sheet #day43 - grep

Cheat Sheet #day43 - grep

grep Cheatsheet

Basic Usage

  • Search for a Pattern in a File

      grep 'pattern' filename
    
  • Search for a Pattern in Multiple Files

      grep 'pattern' file1 file2 file3
    

Common Options

  • Ignore Case Sensitivity

      grep -i 'pattern' filename
    
  • Show Line Numbers

      grep -n 'pattern' filename
    
  • Search Recursively in Directories

      grep -r 'pattern' directory
    
  • Show Only Matching Part of the Line

      grep -o 'pattern' filename
    
  • Count the Number of Matches

      grep -c 'pattern' filename
    
  • Show Lines that Do Not Match the Pattern

      grep -v 'pattern' filename
    

Advanced Options

  • Search for Whole Words

      grep -w 'pattern' filename
    
  • Match the Pattern at the Beginning of a Line

      grep '^pattern' filename
    
  • Match the Pattern at the End of a Line

      grep 'pattern$' filename
    
  • Show N Lines of Context Before the Match

      grep -B N 'pattern' filename
    
  • Show N Lines of Context After the Match

      grep -A N 'pattern' filename
    
  • Show N Lines of Context Around the Match

      grep -C N 'pattern' filename
    

Useful Tips

  • Use Extended Regular Expressions

      grep -E 'pattern' filename
    
  • Use Fixed Strings (Faster for Simple Strings)

      grep -F 'pattern' filename
    
  • Show Only the Names of Files with Matches

      grep -l 'pattern' filename
    
  • Show Only the Names of Files without Matches

      grep -L 'pattern' filename
    
  • Display Help for grep

      grep --help
    
  • Search Using Perl-Compatible Regular Expressions

      grep -P 'pattern' filename
    
  • Colorize the Output

      grep --color=auto 'pattern' filename
    

Examples

  • Search for a Case-Insensitive Pattern in a File

      grep -i 'error' logfile.txt
    
  • Search for a Pattern in All Files in the Current Directory

      grep 'TODO' *
    
  • Search for a Pattern Recursively in a Directory

      grep -r 'TODO' /path/to/directory
    
  • Search for a Whole Word Only

      grep -w 'hello' filename
    
  • Search for a Pattern and Show Line Numbers

      grep -n 'main' script.py
    
  • Search for a Pattern and Show Only Matching Part of the Line

      grep -o 'http[s]*://[^ ]*' filename
    
  • Count the Number of Occurrences of a Pattern

      grep -c 'main' script.py
    
  • Search for a Pattern and Show 3 Lines of Context After the Match

      grep -A 3 'function' script.py
    
  • Search for a Pattern and Show 2 Lines of Context Before the Match

      grep -B 2 'error' logfile.txt
    
  • Search for a Pattern and Show 1 Line of Context Around the Match

      grep -C 1 'TODO' script.py
    
  • Use Extended Regular Expressions to Search for Multiple Patterns

      grep -E 'error|warning|info' logfile.txt
    
  • Search for a Pattern Using Perl-Compatible Regular Expressions

      grep -P '\d{3}-\d{2}-\d{4}' filename
    
  • Search for a Pattern in a Compressed File

      zgrep 'pattern' file.gz
    

This cheatsheet covers essential commands and options for using grep effectively, from basic searching to advanced pattern matching and context display. Adjust the commands according to your specific requirements and environment.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!