Extract lines where cat appears as an independent word.
Exclude concatenate and bobcat, but include cat-1.
Which command is appropriate?
grep -w requires the matching text to be bounded by non-word characters. A hyphen is not a word character, so cat-1 qualifies.
Detailed explanation
grep -F 'cat' words.txtIncorrect. -F finds the substring inside concatenate and bobcat.
Incorrect. -F finds the substring inside concatenate and bobcat.
grep -x 'cat' words.txtIncorrect. -x would require the entire line to be exactly cat.
Incorrect. -x would require the entire line to be exactly cat.
grep -w 'cat' words.txtCorrect. -w excludes embedded words but allows cat-1.
Correct. -w excludes embedded words but allows cat-1.
grep -o 'cat' words.txtIncorrect. -o prints matching portions but does not enforce word boundaries.
Incorrect. -o prints matching portions but does not enforce word boundaries.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' cat concatenate bobcat cat-1 | grep -w 'cat'Expected result
cat
cat-1Key points
- -w matches a whole word
- -x matches a whole line
- A hyphen forms a word boundary
Notes
- Environment: GNU grep 3.x / LC_ALL=C
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.