0

I have file like this

<IfModule mod_unixd.c>
    # Run as a unique, less privileged user for security reasons.
    # User/Group: The name (or #number) of the user/group to run httpd as.
    # Default: User #-1, Group #-1
    # https://httpd.apache.org/docs/current/mod/mod_unixd.html
    # https://en.wikipedia.org/wiki/Principle_of_least_privilege
    User www-data #
    Group www-data
</IfModule>

I use command for removing comments but it doesn't remove comments, what's wrong?

 sed -e '/^[\s]*#/d' test.conf'

Regexp in online

Output must be

<IfModule mod_unixd.c>
    User www-data #
    Group www-data
</IfModule>
Vitalii
  • 11
  • 3
  • Please add your desired output (no description) for that sample input to your question (no comment). – Cyrus Feb 02 '20 at 11:52

2 Answers2

2

Output must be

Looks like you want to remove lines beginning with a # (after zero or more whitespaces) and you don't want to remove comments which begins after the end of non-comment statements (like User www-data #)

Try this:

sed -e '/^\s*#/d' test.conf

Or this:

sed -e '/^[ \t]*#/d' test.conf

This regex will match lines beginning with 0 or more spaces or tabs (in any order), followed by a hash sign.

GNU sed also supports symbolic names:

sed -e '/^[[:space:]]*/d'

Which includes all whitespace characters, including the funny unicode foreign language ones. That's less portable, however.

EDIT:

sed --version on my system returns:

sed (GNU sed) 4.7
Packaged by Debian
Copyright (C) 2018 Free Software Foundation, Inc.
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

To remove any line beginning with whitespace followed by #, you can use

sed -e '/^\W*#/d' test.conf

This means the pattern matches

  • from the beginning of the line
  • allows only whitespace characters
  • followed by #
  • anything after that is ignored
Jonas Eberle
  • 2,835
  • 1
  • 15
  • 25