terminal.cab

All commands

grep

Topic: Searching

Program: grep

Finds lines that match a pattern.

Package: text-tools

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
  • -G, --basic-regexp not in the simulator yet

    On a real system grep then reads the patterns as basic regular expressions, as it does without options.

  • -P, --perl-regexp not in the simulator yet

    On a real system grep reads the patterns as Perl-style regular expressions, for example \d for a digit.

  • --no-ignore-case not in the simulator yet

    On a real system it cancels an earlier -i.

  • -z, --null-data not in the simulator yet

    On a real system a zero byte instead of a newline separates the lines.

  • -s, --no-messages not in the simulator yet

    On a real system grep then keeps quiet about missing or unreadable files.

  • -V, --version not in the simulator yet

    On a real system it prints the version of the program.

  • -b, --byte-offset not in the simulator yet

    On a real system grep puts the byte position in front of each line.

  • --line-buffered not in the simulator yet

    On a real system grep passes on each line at once; the result stays the same.

  • -H, --with-filename not in the simulator yet

    On a real system grep then puts the file name in front of each line, even for one file.

  • --label needs a value not in the simulator yet

    On a real system grep shows this name instead of (standard input).

  • --binary-files needs a value not in the simulator yet

    On a real system it decides how grep treats files with zero bytes.

  • -a, --text not in the simulator yet

    On a real system it decides how grep treats files with zero bytes.

  • -I not in the simulator yet

    On a real system it decides how grep treats files with zero bytes.

  • -d, --directories needs a value not in the simulator yet

    On a real system it decides what grep does with directories.

  • -D, --devices needs a value not in the simulator yet

    On a real system it decides what grep does with device files.

  • -R, --dereference-recursive not in the simulator yet

    On a real system grep also follows symlinks while doing so.

  • --exclude-from needs a value not in the simulator yet

    On a real system grep reads the patterns for --exclude from a file.

  • --exclude-dir needs a value not in the simulator yet

    On a real system grep -r skips directories with a matching name.

  • -T, --initial-tab not in the simulator yet

    On a real system grep lines up the text after the name with a tab.

  • -Z, --null not in the simulator yet

    On a real system each file name is followed by a zero byte, for xargs -0.

  • -0 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -1 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -2 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -3 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -4 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -5 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -6 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -7 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -8 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • -9 not in the simulator yet

    On a real system -NUM is the same as -C NUM: NUM lines around each match.

  • --group-separator needs a value not in the simulator yet

    On a real system it changes the "--" line between groups with context.

  • --no-group-separator not in the simulator yet

    On a real system it changes the "--" line between groups with context.

  • --color value optional not in the simulator yet

    On a real system grep colours the matches; the simulator shows them without colour.

  • --colour value optional not in the simulator yet

    On a real system grep colours the matches; the simulator shows them without colour.

  • -U, --binary not in the simulator yet

    On a real system the option only does something on Windows.

Examples

From the lesson Searching
grep 404 projects/website/server.log
08:05 GET /about.html 404
08:12 GET /contact.html 404
08:27 GET /favicon.ico 404

Try it in the terminal

From the lesson Searching
grep -c 200 projects/website/server.log
9

Try it in the terminal

From the lesson Searching
grep -ri garlic Documents
Documents/recipes.txt:- 2 cloves of garlic
Documents/recipes.txt:Garlic bread
Documents/recipes.txt:- 3 cloves of garlic

Try it in the terminal

From the lesson Chaining commands
grep 404 projects/website/server.log | wc -l
3

Try it in the terminal

From the lesson Chaining commands
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

Try it in the terminal

From the lesson Finding lines, finding differences
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

Try it in the terminal

From the lesson Steering the streams
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

Try it in the terminal

From the lesson Steering the streams
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

Try it in the terminal

From the command of the day: grep -i
grep -i error logs/app.log
ERROR database timeout
INFO retry after ERROR ok
error cache miss
ERROR payment failed

Try it in the terminal

From the command of the day: grep -n
grep -n WARN logs/app.log
3:WARN disk 85% full
7:WARN slow response

Try it in the terminal

From the command of the day: grep -v
grep -v INFO logs/app.log
WARN disk 85% full
ERROR database timeout
error cache miss
WARN slow response
ERROR payment failed

Try it in the terminal

From the command of the day: grep -r
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

Try it in the terminal

From the command of the day: grep -E '^(ERROR|WARN)'
grep -E '^(ERROR|WARN)' logs/app.log
WARN disk 85% full
ERROR database timeout
WARN slow response
ERROR payment failed

Try it in the terminal

Where you learn it

Lessons

Command of the day

All commands