2

I'm trying to uncomment a 4 line section in an (nginx config) file. Using sed. I first tried with grep and the regexp seems to be correct:

$ grep ^#.*bny /etc/nginx/sites-enabled/default
#        location /bny {

However when I try with sed to delete the # at the start of the line it fails:

$ sudo sed -i '/^#.*bny/,+3 s/^#+//' /etc/nginx/sites-enabled/default
$ grep ^#.*bny /etc/nginx/sites-enabled/default
#        location /bny {

I think I use the same tactics for commenting the section with sed and that works just fine:

sudo sed -i '/bny/,+3 s/^/#/' /etc/nginx/sites-enabled/default

Even double commenting is no problem. What am I doing wrong with the uncommenting?

dr jerry
  • 329

1 Answers1

4

+ is literal in basic regular expression (BRE) syntax, so your substitution pattern fails to match.

You can use s/^##*//, s/^#\{1,\}// or (in GNU sed) s/^#\+//, or switch to extended regular expressions (ERE) using the -E or -r command line option.

steeldriver
  • 81,074