When solving common tasks filtering of comments, e.g. I can do so:

grep -v ^# /etc/apache2/apache2.conf

Such way I got not commented and empty lines. That was expected.

I thought that another way is:

grep ^[^#] /etc/apache2/apache2.conf

However I got the same output but without empty lines. What is the reason?

  • @hildred @steeldriver From some testing in perl I saw that inside perl-script our "empty lines" (with only one newline) had length 1. But when I check file through perl one-liner like perl -lane 'print length' /etc/apache2/apache2.conf "empty lines" had zero length (the same as in grep). But it is not entirely clear for me.
    – red0ct
    Jun 20, 2017 at 22:02
  • 1
    In Perl the length of the string may include a newline (if it is there it is counted) but the regex engine ignores trailing newlines unless you explicitly enable newline matching. This was done to have better compatibility with sed and awk.
    – hildred
    Jun 21, 2017 at 14:42

2 Answers 2


The second is looking for lines with at least one character and therefore does not match empty lines which have no characters. If you want both try the following:

grep -e ^[^#] -e ^$ /etc/apache2/apache2.conf
grep -v ^#
  • exclude lines that do not start with #
  • blank lines do not start with #, so are not excluded


grep ^[^#]
  • include lines that start with any character except #
  • blank lines do not start with any character, so are not included

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .