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. A start anchor checks only the beginning and does not reject trailing text.
enabled$Correct. enabled$ requires the word to finish at the line end.
Correct. The end anchor makes enabled the final text on the line.
.enabledIncorrect. . matches any preceding character and does not anchor the end.
Incorrect. A dot consumes one character but does not assert the end of the line.
[enabled]$Incorrect. [enabled] is a one-character class, not the whole word.
Incorrect. The bracket expression matches one of the letters, not the word enabled.
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
- Use temporary files and harmless commands for pattern experiments.
- Regular-expression details should be checked against the target GNU implementation.
Foundation review
Place the condition
An end anchor distinguishes an exact trailing value from a value that merely starts with the same letters.
Check the result
Include enabled and enabled-now in the input and compare the matches.