Practical grep snippets for searching text, filtering logs, and finding patterns in files.
Linux grep CLIgrep "search_term" filename.txt
grep -i "search_term" filename.txt
-i to ignore case. Matches "Error", "error", "ERROR" etc.grep -r "search_term" /path/to/dir/
-r to search all files in a directory recursively.grep -c "search_term" filename.txt
grep -n "search_term" filename.txt
-n to display line numbers alongside matching lines.grep -v "search_term" filename.txt
-v to show lines that do NOT match the pattern.grep "ERROR" /var/log/app.log
grep -A 3 -B 3 "ERROR" /var/log/app.log
-A 3 shows 3 lines after the match. -B 3 shows 3 lines before. Useful for debugging.tail -f /var/log/app.log | grep "ERROR"
grep -E "error|warn|fatal" filename.txt
-E for extended regex. This example matches lines with "error", "warn", or "fatal".grep -rl "search_term" /path/to/dir/
-l to list only filenames that contain the match, not the lines.grep -r "search_term" /path/ --exclude="*.log"
--include to do the opposite.