Skip to content
Latchkey

grep: Search Text for a Pattern in CI

grep prints every line of its input that matches the pattern you give it.

grep is the workhorse for scanning logs and command output in a pipeline. Its exit code, not just its output, is what most CI scripts react to.

What it does

grep reads files (or stdin) line by line and prints the lines that match a basic regular expression. With one file it prints just matching lines; with multiple files it prefixes each match with the filename. It exits 0 if any line matched, 1 if none matched, and 2 on an error such as an unreadable file.

Common usage

Terminal
grep ERROR build.log
cat build.log | grep ERROR
grep "Connection refused" *.log
some-command | grep "deprecated"

Options

FlagWhat it does
patternThe basic regular expression to match
file...One or more files to search (default: stdin)
-iCase-insensitive match
-vInvert: print lines that do not match
-nPrefix each match with its line number

In CI

grep is most useful in a pipeline as a gate: command | grep "ok" succeeds only when the text appears. Remember the output and the exit code are separate signals; a step under set -e reacts to the exit code.

Common errors in CI

The classic surprise is that grep exits 1 when nothing matches. Under set -e (or set -o pipefail) a no-match grep aborts the whole step even though nothing went wrong. Guard it with grep ... || true when a non-match is acceptable. "No such file or directory" with exit 2 means a path is wrong, not a missing match.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →