LPIC-1 Exam 101 free question 8 / 10 · Domain 3: GNU and Unix Commands
The contents of app.conf are five lines, from line 1: '# header', 'port=80', ' # note' (with two leading spaces), '#host=x', and 'user=www'. Which is the correct output of grep -vn '^#' app.conf?
- 1:port=80 / 2: # note / 3:user=www
- 2:port=80 / 5:user=www
- 1:port=80 / 2:user=www
- 2:port=80 / 3: # note / 5:user=www
Show the answer and explanation
Answer: D
Why this is correct
^ is an anchor that matches the empty string at the start of a line, so '^#' matches only lines whose first character is #. -v inverts the match and selects lines that did not match, and -n puts in front of each line printed its line number in the input file. Lines 1 and 4 start with #, so they are dropped, and lines 2, 3, and 5 remain. Line 3 has blanks before the #, so its first character is not #, and it is selected even though it looks like a comment.
Why the other options are wrong
"2:port=80 / 5:user=www" … This assumes ^ skips leading blanks and looks at the first real character. ^ matches only the true start of a line, so to exclude indented comments too, you allow blanks, as in '^[[:space:]]#'.
"1:port=80 / 2: # note / 3:user=www" … This assumes -n renumbers the output lines from 1. The number attached is the line number in the original file, so the skipped numbers show that lines were left out.
"1:port=80 / 2:user=www" … This gets both the skipping of blanks and the renumbering wrong. Neither happens, so the three lines are printed with their original line numbers.
Good to remember
grep -v '^#' is often used to look at a configuration file without its comments, but indented comments and empty lines remain. If you add -c, only a count is printed instead of the lines, and combined with -v it is the number of lines that did not match.
Go to the LPIC-1 Exam 101 practice app (10 free questions; purchase unlocks all questions)