# Cheatsheet & Examples: grep

The `grep` command is a command-line utility for searching text using patterns. It scans files or input streams for lines that match a specified regular expression, outputting those lines. It is widely used for filtering and analyzing text data.

## Search for a pattern in a file  
Example Usage:  
`grep "search_term" filename.txt`  

What it does:  
Searches for the exact string "search_term" in the specified file and prints matching lines.  

Command-line Arguments Explained:  
- "search_term": The string or regular expression to search for.  
- filename.txt: The file to search within.  

## Search for a pattern case-insensitively  
Example Usage:  
`grep -i "error" log.txt`  

What it does:  
Searches for "error" in `log.txt` while ignoring case (e.g., matches "Error", "ERROR", etc.).  

Command-line Arguments Explained:  
- `-i`: Ignores case when matching patterns.  
- "error": The pattern to search for.  
- log.txt: The file to search within.  

## Recursively search in directories  
Example Usage:  
`grep -r "warning" /var/log/`  

What it does:  
Searches all files in the `/var/log/` directory and its subdirectories for lines containing "warning".  

Command-line Arguments Explained:  
- `-r`: Recursively searches through directories.  
- "warning": The pattern to find.  
- `/var/log/`: The directory to search in.  

## Count matching lines  
Example Usage:  
`grep -c "success" results.txt`  

What it does:  
Counts the number of lines in `results.txt` that match "success" and outputs the total.  

Command-line Arguments Explained:  
- `-c`: Counts the number of matching lines.  
- "success": The pattern to count.  
- results.txt: The file to analyze.  

## Show line numbers of matches  
Example Usage:  
`grep -n "debug" file.txt`  

What it does:  
Prints lines containing "debug" from `file.txt`, prefixed with their line numbers.  

Command-line Arguments Explained:  
- `-n`: Displays line numbers for matching lines.  
- "debug": The pattern to locate.  
- file.txt: The file to search.  

## Invert match to exclude lines  
Example Usage:  
`grep -v "fail" output.txt`  

What it does:  
Prints lines from `output.txt` that do **not** contain "fail".  

Command-line Arguments Explained:  
- `-v`: Inverts the match (prints non-matching lines).  
- "fail": The pattern to exclude.  
- output.txt: The file to process.  

## Match whole words only  
Example Usage:  
`grep -w "apple" fruits.txt`  

What it does:  
Finds lines in `fruits.txt` containing the exact word "apple" (not part of another word).  

Command-line Arguments Explained:  
- `-w`: Matches only whole words, not substrings.  
- "apple": The word to search for.  
- fruits.txt: The file to check.  

## Use extended regular expressions  
Example Usage:  
`grep -E "error|warning" file.txt`  

What it does:  
Searches for lines containing either "error" or "warning" using extended regex syntax.  

Command-line Arguments Explained:  
- `-E`: Enables extended regular expressions (like `egrep`).  
- `"error|warning"`: The regex pattern (| = "or").  
- file.txt: The target file.  

## Search in multiple files  
Example Usage:  
`grep "error" file1.txt file2.txt`  

What it does:  
Searches for "error" in both `file1.txt` and `file2.txt`, printing matching lines.  

Command-line Arguments Explained:  
- "error": The pattern to find.  
- file1.txt, file2.txt: Files to search in.  

## Search via pipeline input  
Example Usage:  
`ps aux | grep "python"`  

What it does:  
Filters the output of the `ps aux` command to show only lines containing "python".  

Command-line Arguments Explained:  
- "python": The pattern to match.  
- `ps aux`: Command whose output is piped into `grep`.  

## Highlight matches in color  
Example Usage:  
`grep --color "error" log.txt`  

What it does:  
Prints lines containing "error" with the matched text highlighted in color.  

Command-line Arguments Explained:  
- `--color`: Enables colored output for matches.  
- "error": The pattern to highlight.  
- log.txt: The file to search.  

## Search specific file types recursively  
Example Usage:  
`grep -r --include="*.log" "error" /data/`  

What it does:  
Recursively searches files ending with `.log` in the `/data/` directory for "error".  

Command-line Arguments Explained:  
- `-r`: Recursively searches directories.  
- `--include="*.log"`: Limits search to files with the `.log` extension.  
- "error": The pattern to find.  
- `/data/`: Directory to search in.  

## Show context around matches  
Example Usage:  
`grep -A 2 "warning" file.txt`  

What it does:  
Prints lines containing "warning" along with the two lines *after* each match.  

Command-line Arguments Explained:  
- `-A 2`: Displays 2 lines of "after" context.  
- "warning": The pattern to find.  
- file.txt: The file to check.  

## Search for lines starting with a pattern  
Example Usage:  
`grep "^start" file.txt`  

What it does:  
Finds lines in `file.txt` that begin with "start".  

Command-line Arguments Explained:  
- `"^start"`: Regular expression matching lines starting with "start".  
- file.txt: The file to search.  

## Print filenames with matches  
Example Usage:  
`grep -l "success" *.txt`  

What it does:  
Lists filenames ending with `.txt` that contain at least one line matching "success".  

Command-line Arguments Explained:  
- `-l`: Outputs only filenames with matches.  
- "success": The pattern to find.  
- `*.txt`: Files to search (wildcard for all .txt files).  

## Search for patterns in binary files  
Example Usage:  
`grep -a "magic" binary_file.bin`  

What it does:  
Treats binary files as text and searches for "magic" within them.  

Command-line Arguments Explained:  
- `-a`: Processes binary files as text (avoids "Binary file ... matches" messages).  
- "magic": The pattern to find.  
- binary_file.bin: The binary file to search.  

## Search multiple patterns  
Example Usage:  
`grep -e "error" -e "timeout" file.txt`  

What it does:  
Prints lines from `file.txt` that contain either "error" or "timeout".  

Command-line Arguments Explained:  
- `-e "error"`: First pattern to match.  
- `-e "timeout"`: Second pattern to match.  
- file.txt: The file to search.  

## Use grep with a file of patterns  
Example Usage:  
`grep -f patterns.txt file.txt`  

What it does:  
Searches `file.txt` for lines matching any of the patterns listed in `patterns.txt`.  

Command-line Arguments Explained:  
- `-f patterns.txt`: Reads patterns from the specified file.  
- file.txt: The file to search.
