grep

Search text inside files

Usage

      grep PATTERN FILE
    

Finds lines matching PATTERN in FILE

Common Flags

-i Case-insensitive search-r Recursive search in directories-n Show line numbers--color=auto Highlight matches in color-v Invert match (exclude pattern)-c Count matching lines-l Show only filenames with matches-w Match whole words only-E Use extended regex-A NUM Show NUM lines after match-B NUM Show NUM lines before match-C NUM Show NUM lines before and after

Examples

      grep "text" file.txt
    

Finds lines containing "text" in file.txt

Find all Python files containing a pattern

      grep -r "import pandas" . --include="*.py"
    

Searches for "import pandas" only in .py files

Search with regular expressions

      grep -E "error|warning|critical" logfile.txt
    

Matches lines containing error OR warning OR critical

Find files containing pattern and show filenames only

      grep -rl "TODO" .
    

Lists only the filenames containing "TODO"

Combine with other commands

      ps aux | grep python
    

Searches for Python processes

Search for whole words only

      grep -w "test" file.txt
    

Matches "test" but not "testing" or "latest"