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.
Question
Options
- Agrep '/$' foo
- Bgrep '/#' foo
- Cgrep -v '/$' foo
- Dgrep -v '/#' foo
How the community answered
(32 responses)- A6% (2)
- C91% (29)
- D3% (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.
`grep '/$' foo` would print lines that *do* end with a '/', which is the opposite of the question's requirement.
`grep '/#' foo` searches for lines containing the literal string '/#', and `#` does not represent the end of a line in `grep` regular expressions.
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 '/'.
`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
Community Discussion
No community discussion yet for this question.