nerdexam
LPI

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.

The Power of the Command Line

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)
  • A
    4% (2)
  • B
    14% (7)
  • C
    10% (5)
  • D
    71% (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.

Agrep -v fred data_file

`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`.

Bgrep '[f]red' data_file

`grep '[f]red' data_file` only matches lowercase `fred`; the bracket class `[f]` is equivalent to just `f` and does not include `F`.

Cegrep fred data_file

`egrep fred data_file` performs a case-sensitive match on the exact string `fred` and will not match `Fred`.

Dgrep '[Ff]red' data_fileCorrect

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`.

Egrep -i fred data_fileCorrect

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

#grep#regex#case-insensitive search#pattern matching

Community Discussion

No community discussion yet for this question.

Full 010-150 Practice