grep
Syntax
grep [OPTION]... PATTERN [FILE]...
Parameters
| Parameters | Description | Examples | Level |
|---|---|---|---|
-r --recursive |
grep -r "TODO" src/ |
Common | |
-i --ignore-case |
grep -i "error" log.txt |
Common | |
-n --line-number |
grep -n "function" app.js |
Common | |
-l --files-with-matches |
grep -rl "import" src/ |
Common | |
-v --invert-match |
grep -v "#" config.ini |
Common | |
-c --count |
grep -c "error" log.txt |
Advanced | |
-A |
N | grep -A 3 "Error" log.txt |
Advanced |
-B |
N | grep -B 2 "Error" log.txt |
Advanced |
-E --extended-regexp |
grep -E "error|warning" log.txt |
Common |
Examples
grep -rn "TODO" --include="*.js" .
.js
grep -rn "import" --exclude-dir=node_modules .
grep -E "error|fatal|panic" /var/log/syslog
grep -c "404" access.log
grep -B 2 -A 5 "Exception" app.log
2 + 5
Common Errors
grep: No such file or directory, -r
Tips
- : ripgrep (rg),
- grep -P Perl ()
- : cat log | grep "error" | grep -v "debug"