KKAIROS
Practical guideIllustrative3 min read

grep: search text, with the options worth knowing

grep prints matching lines; exit 0 means a match, 1 no match, 2 an error. No-match is a normal result; reads stdin when no file is given.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-18-A: grep exit status: 0 match, 1 no match, 2 error

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'alpha\nbeta\n' > f.txt

Command:

grep -q alpha f.txt; echo match=$?; grep -q zeta f.txt; echo nomatch=$?; grep -q alpha missing.txt 2>/dev/null; echo error=$?

Recorded output:

match=0
nomatch=1
error=2

TE-18-B: grep reads stdin when no file is given

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'alpha\n' > f.txt

Command:

echo count=$(cat f.txt | grep -c alpha)

Recorded output:

count=1

TE-18-C: grep counts, ignores case and searches a tree; -v inverts the match

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'Error one\nok\nerror two\n' > a.txt; mkdir -p sub; printf 'error three\n' > sub/b.txt

Command:

echo count=$(grep -c -i error a.txt); echo tree=$(grep -r -c -i error . | sort | tr '\n' ' '); echo inverted=$(grep -v -i error a.txt | wc -l)

Recorded output:

count=2
tree=./a.txt:2 ./sub/b.txt:1
inverted=1

TE-18-D: expected non-zero exit: grep -c prints 0 and exits 1 when nothing matches

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'alpha\nbeta\n' > f.txt

Command:

grep -c zzz f.txt

Recorded output:

0

Exit status: 1. This status and the output match what was written before the run.

Reading the recorded output

In TE-18-A the three numbers are the exit statuses of three grep -q runs. The search for alpha in f.txt gave 0, the search for zeta in the same file gave 1, and the search in missing.txt, a file that does not exist, gave 2 with its error message hidden by 2>/dev/null. TE-18-B counted one match when the file was piped in through cat, so grep read standard input. In TE-18-C, a.txt has two lines that contain error in either case, so -c -i printed 2. The recursive count lists a.txt with 2 and sub/b.txt with 1. The last value, inverted=1, means one line is left after -v -i error; by the setup that is the line ok.

TE-18-D: by the setup, f.txt holds the lines alpha and beta. grep -c zzz f.txt printed 0 and exited with status 1, so a count of 0 comes with the no-match status.

Ad

Limits

No-match is a normal result; reads stdin when no file is given.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-18-A | grep exit status: 0 match, 1 no match, 2 error | 0 | PASS | 0472b74f7995 | | TE-18-B | grep reads stdin when no file is given | 0 | PASS | 5a15ccde2184 | | TE-18-C | grep counts, ignores case and searches a tree; -v inverts the match | 0 | PASS | 1bab6eb1ff66 | | TE-18-D | expected non-zero exit: grep -c prints 0 and exits 1 when nothing matches | 1 | PASS | f65411a94d1b |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides