How to uncomment multiple lines on second pattern match using sed?

lines, multiple-instances, pattern-matching, sed

Solution

This might work for you (GNU sed):

sed 'x;/./{x;/#location/,+6s/#/ /;b};x;/#location/h' file

Use the hold space (HS) to store a flag and only act on the address range if the flag has been set.

Problem

I am trying to use sed to uncomment a block of text in this config file. The code I came up with uncomments 7 lines starting from and including the pattern match on the first match but I need it to only work on the second match and skip the first match. ``` sed '/#location.~.*$/,+6s/#/ /' default.conf ``` ``` # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # ``` >

Original source