grep - dwilson2547/wiki_demo GitHub Wiki

Here’s a comprehensive grep cheat sheet with common commands and practical examples:


grep Cheat Sheet

grep (Global Regular Expression Print) is a powerful Linux command for searching text and patterns in files.


1. Basic Syntax

grep [options] "pattern" [file]

2. Common Options

Option Description Example
-i Case-insensitive search grep -i "error" logfile.txt
-v Invert match (exclude pattern) grep -v "success" logfile.txt
-n Show line numbers grep -n "warning" logfile.txt
-c Count matching lines grep -c "404" access.log
-r Recursive search (directories) grep -r "function" /path/to/dir/
-l List files with matches grep -l "main()" *.c
-w Match whole words only grep -w "port" config.txt
-A n Show n lines after match grep -A 3 "error" logfile.txt
-B n Show n lines before match grep -B 2 "error" logfile.txt
-C n Show n lines around match grep -C 1 "error" logfile.txt
-E Extended regex (use with ` , +`, etc.)
-F Fixed string (no regex) grep -F "this exact string" file.txt
--color Highlight matches grep --color "pattern" file.txt

3. Basic Searches

3.1. Search for a String

grep "pattern" file.txt

Example:

grep "hello" example.txt
  • Searches for hello in example.txt.

3.2. Case-Insensitive Search

grep -i "pattern" file.txt

Example:

grep -i "error" logfile.txt
  • Matches error, Error, ERROR, etc.

3.3. Count Matches

grep -c "pattern" file.txt

Example:

grep -c "404" access.log
  • Counts how many times 404 appears in access.log.

3.4. Show Line Numbers

grep -n "pattern" file.txt

Example:

grep -n "warning" system.log
  • Shows line numbers where warning appears.

3.5. Invert Match (Exclude)

grep -v "pattern" file.txt

Example:

grep -v "success" results.txt
  • Shows lines that do not contain success.

3.6. Search in Multiple Files

grep "pattern" file1.txt file2.txt

Example:

grep "TODO" *.py
  • Searches for TODO in all .py files.

3.7. Recursive Search in Directories

grep -r "pattern" /path/to/dir/

Example:

grep -r "include" /usr/include/
  • Recursively searches for include in /usr/include/.

3.8. Whole Word Match

grep -w "word" file.txt

Example:

grep -w "port" config.txt
  • Matches port but not report or export.

3.9. Show Context Around Matches

grep -A 2 -B 2 "pattern" file.txt

Example:

grep -A 3 -B 1 "error" logfile.txt
  • Shows 1 line before and 3 lines after each match.

4. Advanced Searches with Regex

4.1. Match Lines Starting with a Pattern

grep "^pattern" file.txt

Example:

grep "^#" script.sh
  • Finds lines starting with # (comments).

4.2. Match Lines Ending with a Pattern

grep "pattern$" file.txt

Example:

grep ";$" script.sh
  • Finds lines ending with ;.

4.3. Match Specific Patterns

grep "[0-9]" file.txt       # Lines with numbers
grep "[A-Za-z]" file.txt    # Lines with letters
grep "a.*b" file.txt        # Lines with `a` followed by `b`

Example:

grep "^[A-Z]" names.txt
  • Finds lines starting with a capital letter.

4.4. Extended Regex (-E)

grep -E "pattern1|pattern2" file.txt

Example:

grep -E "error|fail|warning" logfile.txt
  • Matches error, fail, or warning.

4.5. Match Email Addresses

grep -E "[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+" file.txt

Example:

grep -E "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" contacts.txt
  • Extracts email addresses.

4.6. Match IP Addresses

grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file.txt

Example:

grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log
  • Extracts IPv4 addresses.

5. Practical Examples

5.1. Find Files Containing a Pattern

grep -l "main()" *.c
  • Lists all .c files containing main().

5.2. Search in Compressed Files

zgrep "pattern" file.gz

Example:

zgrep "error" archive.log.gz
  • Searches inside a .gz file.

5.3. Pipe Output to grep

command | grep "pattern"

Example:

dmesg | grep -i "usb"
  • Filters dmesg output for USB-related messages.

5.4. Search in Command Output

ps aux | grep "nginx"
  • Finds processes related to nginx.

5.5. Highlight Matches

grep --color "pattern" file.txt

Example:

grep --color "TODO" notes.txt
  • Highlights TODO in the output.

5.6. Search for Empty Lines

grep "^$" file.txt
  • Finds empty lines.

5.7. Search for Lines with Only Numbers

grep "^[0-9]*$" file.txt
  • Matches lines with only numbers.

6. Common Use Cases

Task Command
Search for a string grep "text" file.txt
Case-insensitive search grep -i "text" file.txt
Count matches grep -c "pattern" file.txt
Recursive search grep -r "pattern" /path/
Exclude a pattern grep -v "pattern" file.txt
Show line numbers grep -n "pattern" file.txt
Whole word match grep -w "word" file.txt
Search with regex `grep -E "pattern1
Search in compressed files zgrep "pattern" file.gz
Pipe output to grep `ls -l

7. Tips and Tricks

  • Combine with find:
    find /var/log -type f -exec grep -l "error" {} \;
  • Use grep with xargs:
    grep -rl "old-text" /path/ | xargs sed -i 's/old-text/new-text/g'
  • Save results to a file:
    grep "pattern" file.txt > results.txt
  • Search in less: Press / in less to search interactively.
⚠️ **GitHub.com Fallback** ⚠️