grep
Program: grep
Finds lines that match a pattern.
Usage
grep [OPTION]... PATTERNS [FILE]...
Description
Show the lines of each FILE that match PATTERNS. A FILE named -, or no FILE, means the keyboard or pipe (with -r: the current directory). \1 to \9 match the text of a group again.
Options in the simulator
35 more options of the real program, not in the simulator yet
Examples
grep 404 projects/website/server.log
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404 grep -c 200 projects/website/server.log
9 grep -ri garlic Documents
Documents/recipes.txt:- 2 cloves of garlic
Documents/recipes.txt:Garlic bread
Documents/recipes.txt:- 3 cloves of garlic grep 404 projects/website/server.log | wc -l
3 grep 404 projects/website/server.log > errors.txt
cat errors.txt
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404 grep -n ERROR logs/app.log
9:09:27:15 ERROR db timeout after 5000ms
16:09:41:09 ERROR db timeout after 5000ms
grep -c ERROR logs/app.log
2
grep -v INFO logs/app.log
09:19:26 WARN slow query took 1840ms
09:27:15 ERROR db timeout after 5000ms
09:38:44 WARN slow query took 2210ms
09:41:09 ERROR db timeout after 5000ms grep ERROR logs/app.log | wc -l
2
ls -lt logs | head -n 2
total 8
-rw-r--r-- 1 lerner lerner 621 Oct 17 09:41 app.log grep ERROR logs/app.log > errors.txt
wc -l errors.txt
2 errors.txt
echo "checked 09:45" >> errors.txt
wc -l < errors.txt
3 grep -i error logs/app.log
ERROR database timeout
INFO retry after ERROR ok
error cache miss
ERROR payment failed grep -n WARN logs/app.log
3:WARN disk 85% full
7:WARN slow response grep -v INFO logs/app.log
WARN disk 85% full
ERROR database timeout
error cache miss
WARN slow response
ERROR payment failed grep -r ERROR logs
logs/app.log:ERROR database timeout
logs/app.log:INFO retry after ERROR ok
logs/app.log:ERROR payment failed
logs/old.log:ERROR disk full grep -E '^(ERROR|WARN)' logs/app.log
WARN disk 85% full
ERROR database timeout
WARN slow response
ERROR payment failed Where you learn it
Lessons
- Searching Terminal start
- Searching Terminal start
- Searching Terminal start
- Chaining commands Terminal start
- Chaining commands Terminal start
- Finding lines, finding differences Linux Basics, Module 3: Text tools
- Steering the streams Linux Basics, Module 4: Pipes and redirection
- Steering the streams Linux Basics, Module 4: Pipes and redirection