No matching examples. Try another query or clear the filters.
Copy link A visual reference · grep
grep, one change at a time
Select lines, inspect context and search files while keeping matching separate from output.
One small input. One focused change. Its actual result.
The canonical Org file owns the programs and synthetic fixtures. Each complete
standalone Bash script recreates its exact inputs in an owned temporary
directory. The final PDF is versioned at the repository root.
Last changes: 2026-11-01. First edition: 84 deliberately small examples, complete inputs, authored expected outcomes, BSD/GNU comparisons and a separate GNU-extension chapter.
84 worked examples
14 topic families
1 canonical Org source
Browse by code and result
All grep examples at a glance 84 examples · 14 topics
Choose a preview to open the complete program and captured result. Search examples or select a topic to narrow the gallery. An ellipsis marks shortened code or output.
01 - Select your first lines 6 examples
02 - Anchor a basic regular expression 6 examples
03 - Choose extended regex notation 6 examples
04 - Use fixed strings and pattern lists 6 examples
05 - Choose what grep prints 6 examples
06 - Count, stop and interpret status 6 examples
07 - Inspect surrounding context 6 examples
08 - Read files and standard input 6 examples
09 - Search a directory tree 6 examples
10 - Make case, word and line rules explicit 6 examples
11 - Quote patterns and handle filenames 6 examples
12 - Compose small pipeline steps 6 examples
13 - GNU grep extensions 6 examples
14 - Build useful checks from small pieces 6 examples Copy link Reading the tour
Follow one change
Read the input
All files consumed by a command are shown completely. Small inputs make every selected, changed or omitted line visible.
Change one feature
Follow the comparison link. Read the focused command and its named switch or instruction before adding another feature.
Check the actual result
Stdout and the complete driver status are checked against independently authored expectations. Empty stdout is displayed explicitly.
The gallery, search, topic filter and stable GRP IDs support quick lookup.
Copy the focused command while using the shown fixture, or download the complete
script to recreate that fixture. The complete script runs with LC_ALL=C in a
fresh directory and removes that owned directory on exit. In file-editing
examples, only those reconstructed local copies are changed.
Copy link GRP-01-001 Select a line containing a word A match anywhere on a line selects that whole line.
Back to example gallery Code grep 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common blueSearch for these characters anywhere on the line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep 'blue' data/colors.txt
Download: GRP-01-001.sh
Copy link GRP-01-002 Ignore case Add only -i to include the capitalized spelling.
Back to example gallery Compare with GRP-01-001 · Select a line containing a word .
Code grep -i 'blue' data/colors.txt
Actual output Captured run · exit 0
blue
Blue
blueberry
sky blue
Parameter Meaning in this example ImplementationBSD/GNU common -iIgnore letter case for this ASCII input.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -i 'blue' data/colors.txt
Download: GRP-01-002.sh
Copy link GRP-01-003 Require a whole word Return to case-sensitive matching and add -w. The word can still have surrounding text.
Back to example gallery Compare with GRP-01-001 · Select a line containing a word .
Code grep -w 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -wRequire word boundaries around the match.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -w 'blue' data/colors.txt
Download: GRP-01-003.sh
Copy link GRP-01-004 Require the whole line Change -w to -x. Surrounding words now reject a line.
Back to example gallery Compare with GRP-01-003 · Require a whole word .
Code grep -x 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -xRequire the pattern to match the complete input line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -x 'blue' data/colors.txt
Download: GRP-01-004.sh
Copy link GRP-01-005 Invert the selection Change the base search with -v to print its nonmatching lines.
Back to example gallery Compare with GRP-01-001 · Select a line containing a word .
Code grep -v 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -vSelect lines that do not match.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -v 'blue' data/colors.txt
Download: GRP-01-005.sh
Copy link GRP-01-006 Show original line numbers Add presentation information without changing which lines match.
Back to example gallery Compare with GRP-01-001 · Select a line containing a word .
Code grep -n 'blue' data/colors.txt
Actual output Captured run · exit 0
1:blue
3:blueberry
4:sky blue
Parameter Meaning in this example ImplementationBSD/GNU common -nPrefix matches with one-based input line numbers.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -n 'blue' data/colors.txt
Download: GRP-01-006.sh
Copy link GRP-02-001 Match a literal substring The unanchored pattern also selects longer words containing cat.
Back to example gallery Code grep 'cat' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common catOrdinary letters match themselves.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep 'cat' data/words.txt
Download: GRP-02-001.sh
Copy link GRP-02-002 Anchor the start A caret removes matches that begin later in a line.
Back to example gallery Compare with GRP-02-001 · Match a literal substring .
Code grep '^cat' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common ^Require the beginning of the line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep '^cat' data/words.txt
Download: GRP-02-002.sh
Copy link GRP-02-003 Anchor the end Use the end anchor instead of the start anchor.
Back to example gallery Compare with GRP-02-002 · Anchor the start .
Code grep 'cat$' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common $Require the end of the input line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep 'cat$' data/words.txt
Download: GRP-02-003.sh
Copy link GRP-02-004 Anchor both ends Put both boundaries around the same literal word.
Back to example gallery Compare with GRP-02-003 · Anchor the end .
Code grep '^cat$' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common ^cat$Consume exactly this complete line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep '^cat$' data/words.txt
Download: GRP-02-004.sh
Copy link GRP-02-005 Allow one character at the dot Change the middle letter to the BRE wildcard.
Back to example gallery Compare with GRP-02-004 · Anchor both ends .
Code grep '^c.t$' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common .Match one character; grep still processes one line at a time.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep '^c.t$' data/words.txt
Download: GRP-02-005.sh
Copy link GRP-02-006 Match a period literally A bracket expression avoids an additional backslash layer.
Back to example gallery Compare with GRP-02-005 · Allow one character at the dot .
Code grep '^cat[.]$' data/words.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common [.]Treat the period as literal punctuation.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/words.txt <<'GRP_INPUT_1'
cat
scatter
bobcat
cat.
cot
cut
dog
GRP_INPUT_1
grep '^cat[.]$' data/words.txt
Download: GRP-02-006.sh
Copy link GRP-03-001 Turn on extended syntax An ERE alternative matches either word anywhere on a line.
Back to example gallery Code grep -E 'cat|dog' data/animals.txt
Actual output Captured run · exit 0
cat
dog
cats
dogs
catfish
Parameter Meaning in this example ImplementationBSD/GNU common -EEnable ERE syntax before using alternation.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/animals.txt <<'GRP_INPUT_1'
cat
dog
cats
dogs
catfish
GRP_INPUT_1
grep -E 'cat|dog' data/animals.txt
Download: GRP-03-001.sh
Copy link GRP-03-002 Group alternatives before anchoring Only the two complete singular names remain.
Back to example gallery Compare with GRP-03-001 · Turn on extended syntax .
Code grep -E '^(cat|dog)$' data/animals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common (...)Apply both anchors to the grouped alternatives.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/animals.txt <<'GRP_INPUT_1'
cat
dog
cats
dogs
catfish
GRP_INPUT_1
grep -E '^(cat|dog)$' data/animals.txt
Download: GRP-03-002.sh
Copy link GRP-03-003 Make a suffix optional Add an optional s after the same grouped alternatives.
Back to example gallery Compare with GRP-03-002 · Group alternatives before anchoring .
Code grep -E '^(cat|dog)s?$' data/animals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common ?Allow the preceding s zero or one time.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/animals.txt <<'GRP_INPUT_1'
cat
dog
cats
dogs
catfish
GRP_INPUT_1
grep -E '^(cat|dog)s?$' data/animals.txt
Download: GRP-03-003.sh
Copy link GRP-03-004 Require one or more digits Select a complete AB identifier using a repeated ASCII class.
Back to example gallery Code grep -E '^AB-[0-9]+$' data/ids.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common +Require at least one digit.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/ids.txt <<'GRP_INPUT_1'
AB-7
AB-42
AB-123
CD-007
xAB-123
GRP_INPUT_1
grep -E '^AB-[0-9]+$' data/ids.txt
Download: GRP-03-004.sh
Copy link GRP-03-005 Require exactly three digits Replace plus with an exact repetition interval.
Back to example gallery Compare with GRP-03-004 · Require one or more digits .
Code grep -E '^AB-[0-9]{3}$' data/ids.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common {3}Require exactly three digits.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/ids.txt <<'GRP_INPUT_1'
AB-7
AB-42
AB-123
CD-007
xAB-123
GRP_INPUT_1
grep -E '^AB-[0-9]{3}$' data/ids.txt
Download: GRP-03-005.sh
Copy link GRP-03-006 Write the same interval as BRE Remove -E and use BRE interval escaping. The accepted identifier stays the same.
Back to example gallery Compare with GRP-03-005 · Require exactly three digits .
Code grep '^AB-[0-9]\{3\}$' data/ids.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common \{3\}BRE notation for the same repetition interval.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/ids.txt <<'GRP_INPUT_1'
AB-7
AB-42
AB-123
CD-007
xAB-123
GRP_INPUT_1
grep '^AB-[0-9]\{3\}$' data/ids.txt
Download: GRP-03-006.sh
Copy link GRP-04-001 Search a metacharacter literally Fixed-string mode makes plus an ordinary character.
Back to example gallery Code grep -F 'a+b' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -FInterpret the pattern as literal text.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -F 'a+b' data/literals.txt
Download: GRP-04-001.sh
Copy link GRP-04-002 Add another literal term Two -e options select the union of matching lines.
Back to example gallery Compare with GRP-04-001 · Search a metacharacter literally .
Code grep -F -e 'a+b' -e 'a.b' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -eAdd an alternative pattern without regex alternation.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -F -e 'a+b' -e 'a.b' data/literals.txt
Download: GRP-04-002.sh
Copy link GRP-04-003 Move the terms into a pattern file One term per line has the same OR meaning as the preceding -e options.
Back to example gallery Code grep -Ff data/terms.txt data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -fRead one pattern per line from the named file.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/terms.txt <<'GRP_INPUT_1'
a+b
a.b
GRP_INPUT_1
cat > data/literals.txt <<'GRP_INPUT_2'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_2
grep -Ff data/terms.txt data/literals.txt
Download: GRP-04-003.sh
Copy link GRP-04-004 Observe an empty pattern The empty literal occurs on every line, so every line is selected.
Back to example gallery Code grep -F '' data/colors.txt
Actual output Captured run · exit 0
blue
Blue
blueberry
sky blue
red
Parameter Meaning in this example ImplementationBSD/GNU common ''An empty pattern is a pattern that matches everywhere.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -F '' data/colors.txt
Download: GRP-04-004.sh
Copy link GRP-04-005 Distinguish an empty pattern file A file with zero patterns selects no lines. The driver catches status 1.
Back to example gallery Code status = 0
grep -Ff data/none.txt data/colors.txt || status = $?
printf 'exit=%s\n' " $status "
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Zero patternsAn empty file differs from a file containing an empty pattern.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
: > data/none.txt
cat > data/colors.txt <<'GRP_INPUT_2'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_2
status = 0
grep -Ff data/none.txt data/colors.txt || status = $?
printf 'exit=%s\n' " $status "
Download: GRP-04-005.sh
Copy link GRP-04-006 Protect a pattern beginning with a dash Use -e so a leading dash is interpreted as pattern data.
Back to example gallery Code grep -F -e '-error' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -e PATTERNSeparate a pattern from option parsing.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -F -e '-error' data/literals.txt
Download: GRP-04-006.sh
Copy link GRP-05-001 Show line numbers on log records Match ERROR at the beginning and retain source positions.
Back to example gallery Code grep -n '^ERROR' data/events.txt
Actual output Captured run · exit 0
3:ERROR disk
5:ERROR network
Parameter Meaning in this example ImplementationBSD/GNU common -nKeep original one-based line numbers.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep -n '^ERROR' data/events.txt
Download: GRP-05-001.sh
Copy link GRP-05-002 Force a filename prefix -H adds a filename even with one input file.
Back to example gallery Compare with GRP-05-001 · Show line numbers on log records .
Code grep -H '^ERROR' data/events.txt
Actual output Captured run · exit 0
data/events.txt:ERROR disk
data/events.txt:ERROR network
Parameter Meaning in this example ImplementationBSD/GNU common -HAlways print a filename prefix.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep -H '^ERROR' data/events.txt
Download: GRP-05-002.sh
Copy link GRP-05-003 Suppress prefixes across files The -h option removes automatic multi-file filename labels.
Back to example gallery Code grep -h '^ERROR' data/a.log data/b.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -hPrint matching text without filenames.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep -h '^ERROR' data/a.log data/b.log
Download: GRP-05-003.sh
Copy link GRP-05-004 List only files containing a match Each matching file is listed once, regardless of matching-line count.
Back to example gallery Code grep -l '^ERROR' data/a.log data/clean.log data/b.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -lPrint only names of files with at least one selected line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/clean.log <<'GRP_INPUT_2'
INFO quiet
INFO ready
GRP_INPUT_2
cat > data/b.log <<'GRP_INPUT_3'
WARN beta
ERROR network
GRP_INPUT_3
grep -l '^ERROR' data/a.log data/clean.log data/b.log
Download: GRP-05-004.sh
Copy link GRP-05-005 List files with no match Switch from -l to -L using the same file list.
Back to example gallery Compare with GRP-05-004 · List only files containing a match .
Code grep -L '^ERROR' data/a.log data/clean.log data/b.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -LPrint names of files without a selected line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/clean.log <<'GRP_INPUT_2'
INFO quiet
INFO ready
GRP_INPUT_2
cat > data/b.log <<'GRP_INPUT_3'
WARN beta
ERROR network
GRP_INPUT_3
grep -L '^ERROR' data/a.log data/clean.log data/b.log
Download: GRP-05-005.sh
Copy link GRP-05-006 Print the matching portions The common GNU/BSD -o extension prints three occurrences on separate output lines.
Back to example gallery Code grep -Eo 'id=[0-9]+' data/counts.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -oPrint each nonempty matching portion, not the whole line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/counts.txt <<'GRP_INPUT_1'
id=7 id=42
none
id=5
GRP_INPUT_1
grep -Eo 'id=[0-9]+' data/counts.txt
Download: GRP-05-006.sh
Copy link GRP-06-001 Count matching lines Multiple occurrences on the first line contribute only one matching line.
Back to example gallery Compare with GRP-05-006 · Print the matching portions .
Code grep -Ec 'id=[0-9]+' data/counts.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -cCount selected lines rather than occurrences.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/counts.txt <<'GRP_INPUT_1'
id=7 id=42
none
id=5
GRP_INPUT_1
grep -Ec 'id=[0-9]+' data/counts.txt
Download: GRP-06-001.sh
Copy link GRP-06-002 Stop after the first selected line The match limit counts lines, not individual substrings.
Back to example gallery Compare with GRP-06-001 · Count matching lines .
Code grep -m 1 'id=' data/counts.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -m 1Stop reading this file after one selected line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/counts.txt <<'GRP_INPUT_1'
id=7 id=42
none
id=5
GRP_INPUT_1
grep -m 1 'id=' data/counts.txt
Download: GRP-06-002.sh
Copy link GRP-06-003 Allow two selected lines Increase only the line limit.
Back to example gallery Compare with GRP-06-002 · Stop after the first selected line .
Code grep -m 2 'id=' data/counts.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -m 2Allow two selected lines from this input.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/counts.txt <<'GRP_INPUT_1'
id=7 id=42
none
id=5
GRP_INPUT_1
grep -m 2 'id=' data/counts.txt
Download: GRP-06-003.sh
Copy link GRP-06-004 Ask a quiet yes-or-no question Quiet matching writes no match data. The shell prints the branch chosen by grep status.
Back to example gallery Code if grep -q '^ERROR' data/events.txt; then
printf 'errors found\n'
else
printf 'no errors\n'
fi
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -qStop at a successful match and communicate through status.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
if grep -q '^ERROR' data/events.txt; then
printf 'errors found\n'
else
printf 'no errors\n'
fi
Download: GRP-06-004.sh
Copy link GRP-06-005 Capture a no-match status Status 1 is an expected search outcome, not a syntax or file error.
Back to example gallery Compare with GRP-06-004 · Ask a quiet yes-or-no question .
Code status = 0
grep '^NEVER$' data/events.txt || status = $?
printf 'no match: exit=%s\n' " $status "
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Exit 1A valid search selected no lines.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
status = 0
grep '^NEVER$' data/events.txt || status = $?
printf 'no match: exit=%s\n' " $status "
Download: GRP-06-005.sh
Copy link GRP-06-006 Capture a file-error status The missing path is inside the owned directory. The diagnostic is kept local because wording differs across implementations.
Back to example gallery Code status = 0
grep 'anything' absent.txt 2 >diagnostic.txt || status = $?
printf 'file error: exit=%s\n' " $status "
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Exit 2Report a tool error separately from a no-match result.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
status = 0
grep 'anything' absent.txt 2 >diagnostic.txt || status = $?
printf 'file error: exit=%s\n' " $status "
Download: GRP-06-006.sh
Copy link GRP-07-001 Show one line after a match Only the first matching record is used to keep the context change visible.
Back to example gallery Code grep -A 1 'HIT first' data/context.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -A 1Append one following context line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep -A 1 'HIT first' data/context.txt
Download: GRP-07-001.sh
Copy link GRP-07-002 Show one line before a match Change -A to -B.
Back to example gallery Compare with GRP-07-001 · Show one line after a match .
Code grep -B 1 'HIT first' data/context.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -B 1Prepend one preceding context line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep -B 1 'HIT first' data/context.txt
Download: GRP-07-002.sh
Copy link GRP-07-003 Show context on both sides -C combines the same-sized before and after windows.
Back to example gallery Compare with GRP-07-002 · Show one line before a match .
Code grep -C 1 'HIT first' data/context.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -C 1Show one context line on either side.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep -C 1 'HIT first' data/context.txt
Download: GRP-07-003.sh
Copy link GRP-07-004 Distinguish numbered context from matches With -n, a colon labels a matching line and a dash labels context.
Back to example gallery Compare with GRP-07-003 · Show context on both sides .
Code grep -nC 1 'HIT first' data/context.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Number prefixesRead matching and context line labels differently.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep -nC 1 'HIT first' data/context.txt
Download: GRP-07-004.sh
Copy link GRP-07-005 See overlapping windows merge Adjacent matches share their context, so the same source line is not printed twice.
Back to example gallery Code grep -C 1 'HIT' data/near.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Merged contextOverlapping context windows form one output group.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/near.txt <<'GRP_INPUT_1'
one
HIT a
HIT b
four
five
GRP_INPUT_1
grep -C 1 'HIT' data/near.txt
Download: GRP-07-005.sh
Copy link GRP-07-006 See separated context groups Distant matches retain separate groups with the normal separator.
Back to example gallery Code grep -C 1 'HIT' data/context.txt
Actual output Captured run · exit 0
two
HIT first
four
--
six
HIT last
eight
Parameter Meaning in this example ImplementationBSD/GNU common --Separate nonadjacent context groups.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep -C 1 'HIT' data/context.txt
Download: GRP-07-006.sh
Copy link GRP-08-001 Search two explicitly ordered files Automatic prefixes identify each source. Explicit argument order controls the shown sequence.
Back to example gallery Code grep '^ERROR' data/a.log data/b.log
Actual output Captured run · exit 0
data/a.log:ERROR disk
data/b.log:ERROR network
Parameter Meaning in this example ImplementationBSD/GNU common File argumentsProcess the files in the provided argument order.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep '^ERROR' data/a.log data/b.log
Download: GRP-08-001.sh
Copy link GRP-08-002 Count each input file separately -c produces one per-file count with the normal multi-file prefix.
Back to example gallery Compare with GRP-08-001 · Search two explicitly ordered files .
Code grep -c '^ERROR' data/a.log data/b.log
Actual output Captured run · exit 0
data/a.log:1
data/b.log:1
Parameter Meaning in this example ImplementationBSD/GNU common Per-file countDo not mistake several per-file counts for a single total.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep -c '^ERROR' data/a.log data/b.log
Download: GRP-08-002.sh
Copy link GRP-08-003 Read the same log through redirection With no filename argument, grep consumes standard input and has no filename label.
Back to example gallery Code grep '^ERROR' < data/a.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common < FILELet the shell connect a file to standard input.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
grep '^ERROR' < data/a.log
Download: GRP-08-003.sh
Copy link GRP-08-004 Read standard input from a pipeline cat supplies the same bytes as the previous redirection. The extra process is used here only to make the pipeline visible.
Back to example gallery Compare with GRP-08-003 · Read the same log through redirection .
Code cat data/a.log | grep '^ERROR'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Pipeline inputgrep reads bytes produced by the left-hand command.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat data/a.log | grep '^ERROR'
Download: GRP-08-004.sh
Copy link GRP-08-005 Name standard input with a dash A dash explicitly inserts stdin into the file argument list. -h keeps the display independent of stdin label wording.
Back to example gallery Code grep -h '^ERROR' data/a.log - < data/b.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -Read stdin at this position in the input list.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep -h '^ERROR' data/a.log - < data/b.log
Download: GRP-08-005.sh
Copy link GRP-08-006 Reverse the explicit input order Changing only the file order changes the order of the matching records.
Back to example gallery Compare with GRP-08-005 · Name standard input with a dash .
Code grep -h '^ERROR' data/b.log data/a.log
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Argument ordergrep does not sort an explicit list of file arguments.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep -h '^ERROR' data/b.log data/a.log
Download: GRP-08-006.sh
Copy link GRP-09-001 Recurse into a tiny directory tree The final sort stabilizes presentation; filesystem traversal order itself is not promised.
Back to example gallery Code grep -r 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt:target alpha
data/tree/b.tmp:target cache
data/tree/cache/d.txt:target saved
data/tree/sub/c.txt:target gamma
Parameter Meaning in this example ImplementationBSD/GNU common -rRecurse through this tree of ordinary files.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -r 'target' data/tree | sort
Download: GRP-09-001.sh
Copy link GRP-09-002 List recursive matching filenames Replace matched records with file names while retaining deterministic display.
Back to example gallery Compare with GRP-09-001 · Recurse into a tiny directory tree .
Code grep -rl 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt
data/tree/b.tmp
data/tree/cache/d.txt
data/tree/sub/c.txt
Parameter Meaning in this example ImplementationBSD/GNU common -rlCombine recursion with one name per matching file.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -rl 'target' data/tree | sort
Download: GRP-09-002.sh
Copy link GRP-09-003 Include only text filenames The quoted glob is interpreted by grep rather than expanded by the shell.
Back to example gallery Compare with GRP-09-002 · List recursive matching filenames .
Code grep -rl --include= '*.txt' 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt
data/tree/cache/d.txt
data/tree/sub/c.txt
Parameter Meaning in this example ImplementationBSD/GNU common --includeFilter recursive input names using this glob.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -rl --include= '*.txt' 'target' data/tree | sort
Download: GRP-09-003.sh
Copy link GRP-09-004 Exclude temporary filenames Use exclusion to remove only the temporary-file case.
Back to example gallery Compare with GRP-09-002 · List recursive matching filenames .
Code grep -rl --exclude= '*.tmp' 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt
data/tree/cache/d.txt
data/tree/sub/c.txt
Parameter Meaning in this example ImplementationBSD/GNU common --excludeSkip filenames matching the glob.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -rl --exclude= '*.tmp' 'target' data/tree | sort
Download: GRP-09-004.sh
Copy link GRP-09-005 Skip the cache directory A directory exclusion removes its subtree, not every file whose content mentions cache.
Back to example gallery Compare with GRP-09-004 · Exclude temporary filenames .
Code grep -rl --exclude-dir= cache 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt
data/tree/b.tmp
data/tree/sub/c.txt
Parameter Meaning in this example ImplementationBSD/GNU common --exclude-dirSkip directories matching this name.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -rl --exclude-dir= cache 'target' data/tree | sort
Download: GRP-09-005.sh
Copy link GRP-09-006 Combine two independent exclusions Add the temporary-file exclusion to the cache-directory exclusion.
Back to example gallery Compare with GRP-09-005 · Skip the cache directory .
Code grep -rl --exclude-dir= cache --exclude= '*.tmp' 'target' data/tree | sort
Actual output Captured run · exit 0
data/tree/a.txt
data/tree/sub/c.txt
Parameter Meaning in this example ImplementationBSD/GNU common Two exclusionsApply both filename and directory selection rules.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data/tree
mkdir -p data/tree/cache
mkdir -p data/tree/sub
cat > data/tree/a.txt <<'GRP_INPUT_1'
target alpha
quiet
GRP_INPUT_1
cat > data/tree/b.tmp <<'GRP_INPUT_2'
target cache
GRP_INPUT_2
cat > data/tree/sub/c.txt <<'GRP_INPUT_3'
target gamma
GRP_INPUT_3
cat > data/tree/cache/d.txt <<'GRP_INPUT_4'
target saved
GRP_INPUT_4
grep -rl --exclude-dir= cache --exclude= '*.tmp' 'target' data/tree | sort
Download: GRP-09-006.sh
Copy link GRP-10-001 Compare word and substring selection A whole-word test excludes blueberry but retains sky blue.
Back to example gallery Compare with GRP-01-001 · Select a line containing a word .
Code grep -w 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -wMatch a complete word inside a potentially longer line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -w 'blue' data/colors.txt
Download: GRP-10-001.sh
Copy link GRP-10-002 Add case folding to the word test Only case sensitivity changes.
Back to example gallery Compare with GRP-10-001 · Compare word and substring selection .
Code grep -iw 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -iwCombine case-insensitive and whole-word matching.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -iw 'blue' data/colors.txt
Download: GRP-10-002.sh
Copy link GRP-10-003 Use literal whole-line matching The fixed-string mode and whole-line rule solve different parts of this search.
Back to example gallery Code grep -Fx 'a+b' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -FxUse a complete literal line as the matching rule.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -Fx 'a+b' data/literals.txt
Download: GRP-10-003.sh
Copy link GRP-10-004 Select complete ASCII alphabetic lines The driver fixes LC_ALL=C, so the class is interpreted under that locale.
Back to example gallery Code grep -E '^[[:alpha:]]+$' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common [[:alpha:]]Use the alphabetic class in the selected locale.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -E '^[[:alpha:]]+$' data/colors.txt
Download: GRP-10-004.sh
Copy link GRP-10-005 Exclude uppercase ASCII spellings An explicit lowercase range removes Blue from the previous result.
Back to example gallery Compare with GRP-10-004 · Select complete ASCII alphabetic lines .
Code grep -E '^[a-z]+$' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common [a-z]Constrain the accepted ASCII spelling in the C locale.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -E '^[a-z]+$' data/colors.txt
Download: GRP-10-005.sh
Copy link GRP-10-006 Remember that a word is not a field parser The word boundary accepts blue inside sky blue. A whole-line rule can enforce the intended complete value.
Back to example gallery Compare with GRP-10-001 · Compare word and substring selection .
Code grep -wx 'blue' data/colors.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -wxAdd the complete-line constraint to a whole-word search.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/colors.txt <<'GRP_INPUT_1'
blue
Blue
blueberry
sky blue
red
GRP_INPUT_1
grep -wx 'blue' data/colors.txt
Download: GRP-10-006.sh
Copy link GRP-11-001 Preserve a literal space in the pattern Single shell quotes keep the two words in one pattern argument.
Back to example gallery Code grep -F 'red blue' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Shell quotesPreserve the exact single space between the words.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -F 'red blue' data/literals.txt
Download: GRP-11-001.sh
Copy link GRP-11-002 Preserve a literal dollar sign Single quotes prevent shell expansion of HOME in the search term.
Back to example gallery Compare with GRP-11-001 · Preserve a literal space in the pattern .
Code grep -F '$HOME' data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common '$HOME'Pass the literal dollar sign and letters to grep.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
grep -F '$HOME' data/literals.txt
Download: GRP-11-002.sh
Copy link GRP-11-003 Search a literal term stored in a variable Quote the expansion so one variable value becomes one argument.
Back to example gallery Compare with GRP-11-002 · Preserve a literal dollar sign .
Code term = 'a+b'
grep -F " $term " data/literals.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common "$term"Keep variable data in one literal-pattern argument.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
term = 'a+b'
grep -F " $term " data/literals.txt
Download: GRP-11-003.sh
Copy link GRP-11-004 Read a filename containing spaces Create a local copy with the teaching filename, then quote it as one file argument.
Back to example gallery Code cp data/a.log 'notes with spaces.txt'
grep '^ERROR' 'notes with spaces.txt'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Quoted filenameKeep a filename with spaces in one argument.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cp data/a.log 'notes with spaces.txt'
grep '^ERROR' 'notes with spaces.txt'
Download: GRP-11-004.sh
Copy link GRP-11-005 End option parsing before a filename The reconstructed copy begins with a dash. The option terminator prevents it from becoming an option.
Back to example gallery Compare with GRP-11-004 · Read a filename containing spaces .
Code cp data/a.log ./-notes.txt
grep '^ERROR' -- -notes.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common --Terminate option parsing before a leading-dash filename.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cp data/a.log ./-notes.txt
grep '^ERROR' -- -notes.txt
Download: GRP-11-005.sh
Copy link GRP-11-006 Protect both a pattern and a filename Use -e for the leading-dash pattern and -- for the leading-dash input filename.
Back to example gallery Code cp data/literals.txt ./-notes.txt
grep -F -e '-error' -- -notes.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -e and --Assign pattern and filename roles unambiguously.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/literals.txt <<'GRP_INPUT_1'
a+b
aaab
a.b
aXb
red blue
red blue
home=$HOME
-error
GRP_INPUT_1
cp data/literals.txt ./-notes.txt
grep -F -e '-error' -- -notes.txt
Download: GRP-11-006.sh
Copy link GRP-12-001 Require two independent conditions The second grep sees only lines selected by the first.
Back to example gallery Code grep '^ERROR' data/events.txt | grep 'disk'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Two filtersCompose an AND condition across sequential line filters.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep '^ERROR' data/events.txt | grep 'disk'
Download: GRP-12-001.sh
Copy link GRP-12-002 Use two alternatives instead Multiple -e patterns select lines satisfying either condition.
Back to example gallery Compare with GRP-12-001 · Require two independent conditions .
Code grep -e '^WARN' -e '^ERROR' data/events.txt
Actual output Captured run · exit 0
WARN retry
ERROR disk
ERROR network
Parameter Meaning in this example ImplementationBSD/GNU common Alternative patternsSelect WARN OR ERROR records.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep -e '^WARN' -e '^ERROR' data/events.txt
Download: GRP-12-002.sh
Copy link GRP-12-003 Remove one subset after selecting errors The second inverted filter removes network errors from the selected records.
Back to example gallery Compare with GRP-12-002 · Use two alternatives instead .
Code grep '^ERROR' data/events.txt | grep -v 'network'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -v in a pipelineSubtract one subset from an already selected set.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep '^ERROR' data/events.txt | grep -v 'network'
Download: GRP-12-003.sh
Copy link GRP-12-004 Capture an expected pipeline no-match The complete driver enables pipefail. Catch the expected unsuccessful pipeline as a unit.
Back to example gallery Compare with GRP-12-003 · Remove one subset after selecting errors .
Code status = 0
grep '^ERROR' data/events.txt | grep 'missing' || status = $?
printf 'pipeline exit=%s\n' " $status "
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common pipefailPreserve a nonzero pipeline outcome and handle it explicitly.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
status = 0
grep '^ERROR' data/events.txt | grep 'missing' || status = $?
printf 'pipeline exit=%s\n' " $status "
Download: GRP-12-004.sh
Copy link GRP-12-005 Branch on a quiet pipeline result The condition handles a no-match outcome without treating it as an unexpected driver failure.
Back to example gallery Compare with GRP-12-004 · Capture an expected pipeline no-match .
Code if grep '^ERROR' data/events.txt | grep -q 'disk' ; then
printf 'disk error present\n'
fi
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common if pipelineUse the selected result as a shell condition with this tiny finite input.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
if grep '^ERROR' data/events.txt | grep -q 'disk' ; then
printf 'disk error present\n'
fi
Download: GRP-12-005.sh
Copy link GRP-12-006 Extract values after selecting the records grep selects complete records; cut performs the simple delimiter-based extraction.
Back to example gallery Code grep '^PORT=' data/settings.txt | cut -d= -f2
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Selection then extractionGive each tool one clearly specified processing step.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/settings.txt <<'GRP_INPUT_1'
# demo
PORT=8080
# note
HOST=local
GRP_INPUT_1
grep '^PORT=' data/settings.txt | cut -d= -f2
Download: GRP-12-006.sh
Copy link GRP-13-001 Label a standard-input stream GNU --label gives the stdin stream a meaningful name when filename prefixes are forced.
Back to example gallery Code ggrep --label= stream -H '^ERROR' < data/events.txt
Actual output Captured run · exit 0
stream:ERROR disk
stream:ERROR network
Parameter Meaning in this example ImplementationGNU grep --labelName standard input in prefixed GNU output.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/events.txt <<'GRP_INPUT_1'
INFO start
WARN retry
ERROR disk
INFO ready
ERROR network
GRP_INPUT_1
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
ggrep --label= stream -H '^ERROR' < data/events.txt
Download: GRP-13-001.sh
Copy link GRP-13-002 Customize the context-group separator Change only the separator between the two nonadjacent context groups.
Back to example gallery Compare with GRP-07-006 · See separated context groups .
Code ggrep -C 1 --group-separator= NEXT 'HIT' data/context.txt
Actual output Captured run · exit 0
two
HIT first
four
NEXT
six
HIT last
eight
Parameter Meaning in this example ImplementationGNU grep --group-separatorChoose a visible marker between separated groups.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
ggrep -C 1 --group-separator= NEXT 'HIT' data/context.txt
Download: GRP-13-002.sh
Copy link GRP-13-003 Suppress context-group separators Keep the same windows but remove the group marker.
Back to example gallery Compare with GRP-13-002 · Customize the context-group separator .
Code ggrep -C 1 --no-group-separator 'HIT' data/context.txt
Actual output Captured run · exit 0
two
HIT first
four
six
HIT last
eight
Parameter Meaning in this example ImplementationGNU grep --no-group-separatorOmit the marker between context groups.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/context.txt <<'GRP_INPUT_1'
one
two
HIT first
four
five
six
HIT last
eight
nine
GRP_INPUT_1
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
ggrep -C 1 --no-group-separator 'HIT' data/context.txt
Download: GRP-13-003.sh
Copy link GRP-13-004 Terminate listed filenames with NUL The display-only tr converts NUL terminators to newlines. Real filename consumers should retain the NUL framing.
Back to example gallery Code ggrep -lZ '^ERROR' data/a.log data/b.log | tr '\000' '\n'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationGNU grep -ZEnd filename labels with NUL rather than a line terminator.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
ggrep -lZ '^ERROR' data/a.log data/b.log | tr '\000' '\n'
Download: GRP-13-004.sh
Copy link GRP-13-005 Match a NUL-delimited record GNU -z treats the first embedded newline as data within one record. The final pipe is the displayed NUL terminator.
Back to example gallery Code printf 'first\nalpha\000beta\000' | ggrep -z 'alpha' | tr '\000' '|'
printf '\n'
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationGNU grep -zUse NUL as the record separator; make the terminator visible only for display.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
printf 'first\nalpha\000beta\000' | ggrep -z 'alpha' | tr '\000' '|'
printf '\n'
Download: GRP-13-005.sh
Copy link GRP-13-006 Select the PCRE matching mode explicitly This optional GNU example requires grep built with PCRE support. The lookbehind is not POSIX ERE syntax.
Back to example gallery Code ggrep -Po '(?<=id=)[0-9]+' data/counts.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationGNU grep -PSelect GNU grep PCRE mode before using lookbehind.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/counts.txt <<'GRP_INPUT_1'
id=7 id=42
none
id=5
GRP_INPUT_1
grep_gnu_binary = $( command -v ggrep || command -v grep)
grep_gnu_version = $( " $grep_gnu_binary " --version 2 >/dev/null || :)
case " $grep_gnu_version " in
*"GNU grep" *) ;;
*) printf 'This example requires GNU grep.\n' >& 2 ; exit 69 ;;
esac
ggrep() { " $grep_gnu_binary " " $@ " ; }
ggrep -Po '(?<=id=)[0-9]+' data/counts.txt
Download: GRP-13-006.sh
Copy link GRP-14-001 Remove blank and whitespace-only lines One anchored expression defines the complete set of blank records to omit.
Back to example gallery Code grep -v '^[[:space:]]*$' data/settings.txt
Actual output Captured run · exit 0
# demo
PORT=8080
# note
HOST=local
Parameter Meaning in this example ImplementationBSD/GNU common Blank-line predicateOmit only records containing zero or more whitespace characters.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/settings.txt <<'GRP_INPUT_1'
# demo
PORT=8080
# note
HOST=local
GRP_INPUT_1
grep -v '^[[:space:]]*$' data/settings.txt
Download: GRP-14-001.sh
Copy link GRP-14-002 Also remove complete comment lines Add the comment alternative to the blank-line rule. This invented format has no quoting or multiline values.
Back to example gallery Compare with GRP-14-001 · Remove blank and whitespace-only lines .
Code grep -Ev '^[[:space:]]*(#|$)' data/settings.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Comment alternativeTreat indentation followed by # as a comment line.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/settings.txt <<'GRP_INPUT_1'
# demo
PORT=8080
# note
HOST=local
GRP_INPUT_1
grep -Ev '^[[:space:]]*(#|$)' data/settings.txt
Download: GRP-14-002.sh
Copy link GRP-14-003 Keep only assignment-shaped lines Use a positive complete record shape instead of merely discarding comments.
Back to example gallery Compare with GRP-14-002 · Also remove complete comment lines .
Code grep -E '^[A-Z_]+=[^=]+$' data/settings.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common Assignment shapeRecognize this small format; it is not a general configuration parser.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/settings.txt <<'GRP_INPUT_1'
# demo
PORT=8080
# note
HOST=local
GRP_INPUT_1
grep -E '^[A-Z_]+=[^=]+$' data/settings.txt
Download: GRP-14-003.sh
Copy link GRP-14-004 Filter identifiers through an exact allowlist Fixed-string whole-line matching avoids substring acceptance and regex interpretation of list entries.
Back to example gallery Code grep -Fx -f data/allow.txt data/ids.txt
Actual output Captured run · exit 0
Parameter Meaning in this example ImplementationBSD/GNU common -Fx -fTreat each allowlist entry as a complete literal value.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/allow.txt <<'GRP_INPUT_1'
AB-123
CD-007
GRP_INPUT_1
cat > data/ids.txt <<'GRP_INPUT_2'
AB-7
AB-42
AB-123
CD-007
xAB-123
GRP_INPUT_2
grep -Fx -f data/allow.txt data/ids.txt
Download: GRP-14-004.sh
Copy link GRP-14-005 Report acceptance for every candidate Unlike grep selection, the shell loop emits a decision for both accepted and rejected inputs.
Back to example gallery Code while IFS = read -r value; do
if printf '%s\n' " $value " | grep -Eq '^[A-Z]{2}-[0-9]{3}$' ; then
printf 'accept %s\n' " $value "
else
printf 'reject %s\n' " $value "
fi
done < data/ids.txt
Actual output Captured run · exit 0
reject AB-7
reject AB-42
accept AB-123
accept CD-007
reject xAB-123
Parameter Meaning in this example ImplementationBSD/GNU common Complete-value checkKeep rejected near-matches visible in a validation report.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/ids.txt <<'GRP_INPUT_1'
AB-7
AB-42
AB-123
CD-007
xAB-123
GRP_INPUT_1
while IFS = read -r value; do
if printf '%s\n' " $value " | grep -Eq '^[A-Z]{2}-[0-9]{3}$' ; then
printf 'accept %s\n' " $value "
else
printf 'reject %s\n' " $value "
fi
done < data/ids.txt
Download: GRP-14-005.sh
Copy link GRP-14-006 Produce an error-location report Combine already learned matching, filename and line-number controls without adding a new regex feature.
Back to example gallery Code grep -nH '^ERROR' data/a.log data/b.log
Actual output Captured run · exit 0
data/a.log:2:ERROR disk
data/b.log:2:ERROR network
Parameter Meaning in this example ImplementationBSD/GNU common -nHRetain both source filename and original line position in the report.
Complete runnable example set -euo pipefail
export LC_ALL = C
grep_work = $( mktemp -d " ${ TMPDIR :- /tmp } /grep-example.XXXXXX" )
trap 'rm -rf -- "$grep_work"' EXIT
cd " $grep_work "
mkdir -p data
cat > data/a.log <<'GRP_INPUT_1'
INFO alpha
ERROR disk
GRP_INPUT_1
cat > data/b.log <<'GRP_INPUT_2'
WARN beta
ERROR network
GRP_INPUT_2
grep -nH '^ERROR' data/a.log data/b.log
Download: GRP-14-006.sh
Example and command index