nerdexam
Linux_Foundation

LFCS · Question #802

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. This question tests the use of grep for pattern matching and inversion, specifically identifying lines that do not end with a particular character.

Submitted by tarun92· Apr 18, 2026Essential 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

(32 responses)
  • A
    6% (2)
  • C
    91% (29)
  • D
    3% (1)

Why each option

This question tests the use of `grep` for pattern matching and inversion, specifically identifying lines that do not end with a particular character.

Agrep '/$' foo

`grep '/$' foo` would print lines that *do* end with a '/', which is the opposite of the question's requirement.

Bgrep '/#' foo

`grep '/#' foo` searches for lines containing the literal string '/#', and `#` does not represent the end of a line in `grep` regular expressions.

Cgrep -v '/$' fooCorrect

The `grep -v` command is used to invert the match, meaning it prints lines that *do not* match the specified pattern. The regular expression `/$` matches lines that end with a forward slash (`/`), as `$` signifies the end of a line. Therefore, `grep -v '/$' foo` prints all lines from the file 'foo' that do not end with a '/'.

Dgrep -v '/#' foo

`grep -v '/#' foo` would print lines that *do not* contain the literal string '/#', which does not fulfill the condition of not ending with a '/'.

Concept tested: grep pattern matching and inversion

Source: https://man7.org/linux/man/man1/grep.1.html

Topics

#grep#regular expressions#command-line utilities#text filtering

Community Discussion

No community discussion yet for this question.

Full LFCS Practice