Extract only the line service.v1 from versions.txt.
Treat the period as a literal period rather than any one character.
Which command is correct?
In a regular expression, . matches any single character. Escape it as \. to match a literal period.
Detailed explanation
grep '^service.v1$' versions.txtIncorrect. The unescaped period can match a character other than a period.
Incorrect. The unescaped period can match a character other than a period.
grep -F '^service.v1$' versions.txtIncorrect. -F treats the anchors as literal characters, so it does not express the intended whole-line pattern.
Incorrect. -F treats the anchors as literal characters, so it does not express the intended whole-line pattern.
grep '^service\.v1$' versions.txtCorrect. The escaped period matches the literal punctuation and anchors cover the whole line.
Correct. The escaped period matches the literal punctuation and anchors cover the whole line.
grep '^service[.]*v1$' versions.txtIncorrect. [.]* permits zero or more periods and does not require exactly one literal period.
Incorrect. [.]* permits zero or more periods and does not require exactly one literal period.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' service.v1 servicexv1 servicev1 | grep '^service\.v1$'Expected result
service.v1Key points
- . matches any one character
- \. matches a literal period
- ^ and $ require a whole-line match
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.