Extract lines in services.txt that end with enabled.
Exclude enabled-now and lines with later characters.
Which regular expression is appropriate?
$ anchors the position at the end of a line, so enabled$ rejects any later characters.
Detailed explanation
^enabledIncorrect. ^enabled checks the line beginning instead of the ending.
Incorrect. ^enabled checks the line beginning instead of the ending.
enabled$Correct. enabled$ requires the word to finish at the line end.
Correct. enabled$ requires the word to finish at the line end.
.enabledIncorrect. . matches any preceding character and does not anchor the end.
Incorrect. . matches any preceding character and does not anchor the end.
[enabled]$Incorrect. [enabled] is a one-character class, not the whole word.
Incorrect. [enabled] is a one-character class, not the whole word.
Try it yourself
An example you can run in a temporary verification environment.
printf 'api enabled\ndb enabled-now\nweb disabled\n' | grep 'enabled$'Expected result
api enabledKey points
- $ anchors the line end
- Do not use a character class for a word
- Trailing text is excluded
Notes
- Environment: GNU grep 3.x
- 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.