6

I have a sed command that removes comments in a file as,

sed -i /^#/d /path/to/file

This works but not when the comments are indented/have a preceding space.

like

#this is a good comment           ---- works
    #this is an indented comment  ---- doesn't work

How can i change it to remove lines that has # as the first visible character?

4 Answers4

9

Modify your regex so that it allows for leading whitespace.

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

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.

0

try

sed -i -e '/^[ \t]*#/d' /path/to/file

You might have to replace \t by a literal tab.

ikrabbe
  • 2,163
0

You can do :

sed '/^[[:blank:]]*#/d' file.txt

Using grep :

grep -v '^[[:blank:]]*#' file.txt
heemayl
  • 56,300
0

Use:

sed '/^\s*#/d'

Explanation:

^ beginning of line
\s* arbitrary amount of whitespace (or none)
Petr Skocik
  • 28,816