LX0-103 · Question #202
Which grep command will print only the lines that do not end with a / in the file foo?
The correct answer is C. grep -v '/$' foo. The grep -v flag inverts the match to print only lines that do NOT match the given pattern, and '/$' matches lines ending with a forward slash.
Question
Which grep command will print only the lines that do not end with a / in the file foo?
Options
- Agrep '/$' foo
- Bgrep '/#' foo
- Cgrep -v '/$' foo
- Dgrep -v '/#' foo
How the community answered
(42 responses)- A2% (1)
- B2% (1)
- C86% (36)
- D10% (4)
Why each option
The grep -v flag inverts the match to print only lines that do NOT match the given pattern, and '/$' matches lines ending with a forward slash.
This prints lines that DO end with '/' because it lacks the '-v' inversion flag.
The pattern '/#' does not represent end-of-line; '#' is a literal character, so this matches lines containing '/#' rather than lines ending with '/'.
The '-v' flag in grep inverts the selection, printing lines that do not match the pattern. The pattern '/$' uses '$' as the end-of-line anchor, so combined with '-v' this prints every line that does not end with '/'.
While '-v' correctly inverts the match, the pattern '/#' is incorrect - it looks for a literal '/#' sequence rather than lines ending with '/'.
Concept tested: grep inverted matching with end-of-line anchor
Source: https://www.gnu.org/software/grep/manual/grep.html
Topics
Community Discussion
No community discussion yet for this question.