Extract lines that start with a, contain one or more b characters, and end with c.
Use grep extended regular expressions.
Which command is appropriate?
In an ERE, + means one or more repetitions of the preceding element. Unlike *, it does not allow zero occurrences.
Detailed explanation
grep -E '^ab*c$' tokens.txtIncorrect. * also matches abc with zero b characters, which violates the requirement.
Incorrect. * also matches abc with zero b characters, which violates the requirement.
grep -E '^ab?c$' tokens.txtIncorrect. ? allows zero or one b, not one or more.
Incorrect. ? allows zero or one b, not one or more.
grep -F '^ab+c$' tokens.txtIncorrect. -F treats the expression as literal text.
Incorrect. -F treats the expression as literal text.
grep -E '^ab+c$' tokens.txtCorrect. -E enables + and the anchors require the complete line.
Correct. -E enables + and the anchors require the complete line.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' ac abc abbc abdc | grep -E '^ab+c$'Expected result
abc
abbcKey points
- + means one or more
- * means zero or more
- -E enables ERE syntax
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.