Display config.txt lines that are not comments beginning with optional whitespace followed by #.
Keep blank and normal configuration lines.
Which command is appropriate?
grep -v selects lines that do not match. The pattern allows indentation before the comment marker.
Detailed explanation
grep '^[[:space:]]*#' config.txtIncorrect. This selects comment lines rather than excluding them.
Incorrect. This selects comment lines rather than excluding them.
grep -v '#$' config.txtIncorrect. It excludes only lines whose final character is #.
Incorrect. It excludes only lines whose final character is #.
grep -v '^[[:space:]]*#' config.txtCorrect. -v removes lines matching the optional-indent comment pattern.
Correct. -v removes lines matching the optional-indent comment pattern.
grep '^#[[:space:]]*' config.txtIncorrect. It would select only comments with no leading whitespace and is not an inverse filter.
Incorrect. It would select only comments with no leading whitespace and is not an inverse filter.
Try it yourself
An example you can run in a temporary verification environment.
printf '# top\n # indented\nport=80\n\n' | grep -v '^[[:space:]]*#'Expected result
port=80
(続いて空行)Key points
- -v selects nonmatches
- * means zero or more
- Leading whitespace is allowed
Notes
- Environment: GNU grep 3.x / C.UTF-8ロケール
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.