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. It selects comments instead of excluding them.
grep -v '#$' config.txtIncorrect. It excludes only lines whose final character is #.
Incorrect. It excludes only lines ending in #, not the usual line-start comment form.
grep -v '^[[:space:]]*#' config.txtCorrect. -v removes lines matching the optional-indent comment pattern.
Correct. The pattern recognizes optional leading whitespace and -v removes those lines.
grep '^#[[:space:]]*' config.txtIncorrect. It would select only comments with no leading whitespace and is not an inverse filter.
Incorrect. It misses indented comments and prints matching comments rather than excluding them.
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ロケール
- Use temporary files and harmless commands for pattern experiments.
- Regular-expression details should be checked against the target GNU implementation.
Foundation review
Place the condition
The comment pattern must allow indentation, while -v keeps ordinary lines and blank lines.
Check the result
Include an indented comment, a setting, and a blank line in the sample file.