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.
Question
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)- A3% (1)
- B10% (3)
- C7% (2)
- D79% (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.
The -v flag inverts the match, printing only lines that do NOT contain the pattern - the opposite of the desired result.
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.
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.
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.
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
Community Discussion
No community discussion yet for this question.