# Cheat Sheet #day43 - grep

## `grep` Cheatsheet

### Basic Usage

* **Search for a Pattern in a File**
    
    ```bash
    grep 'pattern' filename
    ```
    
* **Search for a Pattern in Multiple Files**
    
    ```bash
    grep 'pattern' file1 file2 file3
    ```
    

### Common Options

* **Ignore Case Sensitivity**
    
    ```bash
    grep -i 'pattern' filename
    ```
    
* **Show Line Numbers**
    
    ```bash
    grep -n 'pattern' filename
    ```
    
* **Search Recursively in Directories**
    
    ```bash
    grep -r 'pattern' directory
    ```
    
* **Show Only Matching Part of the Line**
    
    ```bash
    grep -o 'pattern' filename
    ```
    
* **Count the Number of Matches**
    
    ```bash
    grep -c 'pattern' filename
    ```
    
* **Show Lines that Do Not Match the Pattern**
    
    ```bash
    grep -v 'pattern' filename
    ```
    

### Advanced Options

* **Search for Whole Words**
    
    ```bash
    grep -w 'pattern' filename
    ```
    
* **Match the Pattern at the Beginning of a Line**
    
    ```bash
    grep '^pattern' filename
    ```
    
* **Match the Pattern at the End of a Line**
    
    ```bash
    grep 'pattern$' filename
    ```
    
* **Show N Lines of Context Before the Match**
    
    ```bash
    grep -B N 'pattern' filename
    ```
    
* **Show N Lines of Context After the Match**
    
    ```bash
    grep -A N 'pattern' filename
    ```
    
* **Show N Lines of Context Around the Match**
    
    ```bash
    grep -C N 'pattern' filename
    ```
    

### Useful Tips

* **Use Extended Regular Expressions**
    
    ```bash
    grep -E 'pattern' filename
    ```
    
* **Use Fixed Strings (Faster for Simple Strings)**
    
    ```bash
    grep -F 'pattern' filename
    ```
    
* **Show Only the Names of Files with Matches**
    
    ```bash
    grep -l 'pattern' filename
    ```
    
* **Show Only the Names of Files without Matches**
    
    ```bash
    grep -L 'pattern' filename
    ```
    
* **Display Help for** `grep`
    
    ```bash
    grep --help
    ```
    
* **Search Using Perl-Compatible Regular Expressions**
    
    ```bash
    grep -P 'pattern' filename
    ```
    
* **Colorize the Output**
    
    ```bash
    grep --color=auto 'pattern' filename
    ```
    

### Examples

* **Search for a Case-Insensitive Pattern in a File**
    
    ```bash
    grep -i 'error' logfile.txt
    ```
    
* **Search for a Pattern in All Files in the Current Directory**
    
    ```bash
    grep 'TODO' *
    ```
    
* **Search for a Pattern Recursively in a Directory**
    
    ```bash
    grep -r 'TODO' /path/to/directory
    ```
    
* **Search for a Whole Word Only**
    
    ```bash
    grep -w 'hello' filename
    ```
    
* **Search for a Pattern and Show Line Numbers**
    
    ```bash
    grep -n 'main' script.py
    ```
    
* **Search for a Pattern and Show Only Matching Part of the Line**
    
    ```bash
    grep -o 'http[s]*://[^ ]*' filename
    ```
    
* **Count the Number of Occurrences of a Pattern**
    
    ```bash
    grep -c 'main' script.py
    ```
    
* **Search for a Pattern and Show 3 Lines of Context After the Match**
    
    ```bash
    grep -A 3 'function' script.py
    ```
    
* **Search for a Pattern and Show 2 Lines of Context Before the Match**
    
    ```bash
    grep -B 2 'error' logfile.txt
    ```
    
* **Search for a Pattern and Show 1 Line of Context Around the Match**
    
    ```bash
    grep -C 1 'TODO' script.py
    ```
    
* **Use Extended Regular Expressions to Search for Multiple Patterns**
    
    ```bash
    grep -E 'error|warning|info' logfile.txt
    ```
    
* **Search for a Pattern Using Perl-Compatible Regular Expressions**
    
    ```bash
    grep -P '\d{3}-\d{2}-\d{4}' filename
    ```
    
* **Search for a Pattern in a Compressed File**
    
    ```bash
    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.
