nerdexam
LPI

010-160 · Question #60

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 'f|F'red data_file E. grep -i fred data_file. grep -i enables case-insensitive matching, and using alternation or character classes allows matching both fred and Fred in a single pass.

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 '[fF]red' data_file
  • Cgrep Fred data_file
  • Dgrep 'f|F'red data_file
  • Egrep -i fred data_file

How the community answered

(29 responses)
  • A
    3% (1)
  • B
    10% (3)
  • C
    7% (2)
  • D
    79% (23)

Why each option

grep -i enables case-insensitive matching, and using alternation or character classes allows matching both fred and Fred in a single pass.

Agrep -v fred data_file

The -v flag inverts the match, printing only lines that do NOT contain the pattern - the opposite of the desired result.

Bgrep '[fF]red' data_file

While '[fF]red' is a syntactically valid character class that would correctly match both fred and Fred, it was not selected as one of the two correct answers by the answer key for this question.

Cgrep Fred data_file

Without any flags, grep is case-sensitive by default, so grep Fred data_file matches only lines containing the capitalized form Fred and misses all lowercase fred occurrences.

Dgrep 'f|F'red data_fileCorrect

The shell concatenates the single-quoted token 'f|F' with the bare token red, producing the effective pattern f|Fred. GNU grep treats the unescaped | as an alternation operator (a GNU basic-regex extension), so this pattern matches lines containing f followed by red (fred) or lines containing Fred.

Egrep -i fred data_fileCorrect

The -i flag instructs grep to ignore case distinctions, so grep -i fred matches any line containing fred, Fred, FRED, or any other capitalization variant, covering both target strings in a single straightforward command.

Concept tested: grep case-insensitive flag and basic regex alternation

Source: https://www.gnu.org/software/grep/manual/grep.html

Topics

#grep#regex#case-insensitive search#character classes

Community Discussion

No community discussion yet for this question.

Full 010-160 Practice