Skip to content
Latchkey

grep -w and -x: Whole-Word and Whole-Line Match

grep -w requires the pattern to match a whole word; grep -x requires it to match the entire line.

Partial matches cause false positives (matching "test" inside "latest"). -w and -x tighten the match to a word or a full line.

What it does

grep -w (--word-regexp) matches only when the pattern is bounded by non-word characters, so it matches the word and not a substring of a longer word. grep -x (--line-regexp) matches only when the pattern matches the entire line, end to end. Both work with -F, -E, and -i.

Common usage

Terminal
# match the word "test", not "latest" or "testing"
grep -w test pipeline.log

# match a line that is exactly "PASS"
grep -x "PASS" results.txt

# whole-word, case-insensitive
grep -wi "error" app.log

Options

FlagWhat it does
-w / --word-regexpMatch the pattern only as a whole word
-x / --line-regexpMatch only if the pattern equals the whole line
-FCombine with fixed-string matching
-iCase-insensitive word/line match

In CI

Use -x for status assertions where a line should be exactly a value (e.g. a health check returning exactly "OK"). Use -w to count or detect a specific token without matching it inside other identifiers, which keeps error counts accurate.

Common errors in CI

A word boundary is defined by non-word characters, so grep -w 1.2 may behave unexpectedly because . is a word boundary; quote and consider -F. With -x, trailing whitespace or a carriage return (\r from Windows line endings) makes the line not match exactly; strip CR first (e.g. tr -d "\r") on CRLF input.

Related guides

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