Extract only lines containing exactly three digits from nums.txt.
Use grep's default basic regular expressions; do not add -E.
Which pattern is correct?
In a BRE, repetition counts are written as \{m\}. Combine the expression with ^ and $ to require exactly three digits on the line.
Detailed explanation
^[[:digit:]]{3}$Incorrect. Unescaped {3} is not the portable BRE repetition syntax requested.
Incorrect. Unescaped {3} is not the portable BRE repetition syntax requested.
[[:digit:]]\{3\}Incorrect. It lacks anchors, so a three-digit substring inside a longer line could match.
Incorrect. It lacks anchors, so a three-digit substring inside a longer line could match.
^[[:digit:]]\{3\}$Correct. The escaped BRE interval and anchors require exactly three digits.
Correct. The escaped BRE interval and anchors require exactly three digits.
^[[:digit:]]\+3$Incorrect. \+ means one or more in a BRE and does not express exactly three digits.
Incorrect. \+ means one or more in a BRE and does not express exactly three digits.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' 12 123 1234 abc | grep '^[[:digit:]]\{3\}$'Expected result
123Key points
- BRE uses \{3\}
- ERE uses {3}
- Anchors prevent partial matches
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.