nerdexam
CompTIA

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.

GNU and Unix Commands

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)
  • A
    2% (1)
  • B
    2% (1)
  • C
    86% (36)
  • D
    10% (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.

Agrep '/$' foo

This prints lines that DO end with '/' because it lacks the '-v' inversion flag.

Bgrep '/#' foo

The pattern '/#' does not represent end-of-line; '#' is a literal character, so this matches lines containing '/#' rather than lines ending with '/'.

Cgrep -v '/$' fooCorrect

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 '/'.

Dgrep -v '/#' foo

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

#grep#regex#pattern matching#text filtering

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice