A colleague needs to search a file for the literal string `1.2.3.4` and is worried the dots will match any character. Which command treats the pattern as a fixed string with no regular expression meaning at all?
LPIC-1 Exam 101-500, objective 103. GNU and Unix commands medium
Draft — not checked yet. This question was written from the published exam objectives and cites the clause it comes from, but nobody has yet read it against that clause and signed for it.
It is kept out of search results and out of mock exams until they have. Read the source below before you take the answer as settled.
The options
Not correct grep -x '1.2.3.4' file
Wrong. -x requires the pattern to match the entire line. Again the dots keep their regular expression meaning.
Not correct grep -E '1.2.3.4' file
Wrong. -E selects extended regular expressions, in which an unescaped dot still matches any single character. It makes the problem no better.
Not correct grep -w '1.2.3.4' file
Wrong. -w requires the match to fall on whole-word boundaries. The pattern is still interpreted as a regular expression, so the dots remain wildcards.
Correct grep -F '1.2.3.4' file
Correct. -F (the same as the historical fgrep) treats the pattern as fixed text, so every character including the dots is literal.
Why
grep -F (fgrep) disables regular expressions entirely and is also the fastest mode. grep -E (egrep) selects extended regular expressions. Plain grep uses basic regular expressions. The separate fgrep and egrep binaries still exist on most systems but are deprecated in favour of the -F and -E options.
Where this comes from
- Cited
- LPI exam objective 103.7
- What it says
- Use grep, egrep and fgrep appropriately.
Practise this
Reading one question is not practice. The trainer will draw a set from objective 103 and space the ones you get wrong.