010-150 · Question #62
Which of the following commands will output all of the lines that contain either the string Fred or fred? (Choose TWO correct answers.)
The correct answer is D. grep '[Ff]red' data_file E. grep -i fred data_file. To match both Fred and fred, you need either case-insensitive matching or a character class pattern; -v inverts the match and is incorrect.
Question
Which of the following commands will output all of the lines that contain either the string Fred or fred? (Choose TWO correct answers.)
Options
- Agrep -v fred data_file
- Bgrep '[f]red' data_file
- Cegrep fred data_file
- Dgrep '[Ff]red' data_file
- Egrep -i fred data_file
How the community answered
(49 responses)- A4% (2)
- B14% (7)
- C10% (5)
- D71% (35)
Why each option
To match both `Fred` and `fred`, you need either case-insensitive matching or a character class pattern; `-v` inverts the match and is incorrect.
`grep -v fred data_file` uses the invert flag, which outputs lines that do NOT contain `fred`, and is also case-sensitive so it would miss `Fred`.
`grep '[f]red' data_file` only matches lowercase `fred`; the bracket class `[f]` is equivalent to just `f` and does not include `F`.
`egrep fred data_file` performs a case-sensitive match on the exact string `fred` and will not match `Fred`.
The pattern `[Ff]red` uses a bracket expression (character class) that matches either `F` or `f` followed by `red`, correctly matching both `Fred` and `fred`.
The `-i` flag makes `grep` case-insensitive, so `grep -i fred data_file` matches `fred`, `Fred`, `FRED`, and any other case variant.
Concept tested: grep case-insensitive matching and character classes
Source: https://www.gnu.org/software/grep/manual/grep.html
Topics
Community Discussion
No community discussion yet for this question.