Which grep command will print only the lines that do not end with a / in the file foo?
Correct Answer: C
Explanation
Thegrepcommandthatwillprintonlythelinesthatdonotendwitha/inthefilefooisgrepv/' foo. This command uses the following options and pattern:
-v: Inverts the matching, meaning that it only outputs the lines that do not match the pattern.
/:Matchesa/characterattheendofaline.The symbol represents the end of a line in regular expressions. foo: The name of the file to search.
The output of this command will show all the lines in the file foo that do not have a / as the last character. For example, if the file foo contains the following lines:
/home/user/ /var/log/messages /etc/passwd /usr/bin/
The output of the command will be:
/var/log/messages /etc/passwd
The other commands are incorrect for the following reasons:
* grep '/$' foo: This command will print only the lines that do end with a / in the file foo, which is the opposite of what is required.
* grep '/#' foo: This command will print only the lines that contain the string /# in the file foo, which is not related to the question.
* grep -v '/#' foo: This command will print only the lines that do not contain the string /# in the file foo, which is also not related to the question.
References:
* [LPI Exam 101 Detailed Objectives], Topic 103: GNU and Unix Commands, Objective 103.7: Perform basic file management, Weight: 4, Key Knowledge Areas: Use ofegrepto search for extended regular expressions in text output.
* [How to Use the grep Command on Linux], Topic: Start and End of Lines.