How to remove # in front of a line with sed?

Viewed 193

I'm trying to uncomment (removing #) the line sql.

session {
        radutmp

        #
        #  See "Simultaneous Use Checking Queries" in sql.conf
#       sql
}

This is what I've tried:

sed -i 's/#       sql/        sql/' /etc/freeradius/sites-available/inner-tunnel

But somehow the spaces are not correctly calculated. It doesn't find # sql.

2 Answers

Following melpomene it looks like the input file is using tabs instead of spaces. You can use the [:blank:] character class which includes both tabs and spaces:

sed '/#[[:blank:]]*sql/s/#//' file

The command matches lines with the sequence #, zero or more spaces followed by the term sql and removes the first #

Using sed -E option:

> sed -E 's/#(\s+sql)/\1/i' /etc/freeradius/sites-available/inner-tunnel
                      ^ holds matching chunk from within first capturing group

\s means a character class which denotes [\t\n\r\f\v ] characters. \s+ ensures that there should be some kind of spaces following #

Related