我想sed取消注释并在我的ngnix配置中更改几行
这..
...
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# }
...
..应该变成这个:
...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
...
这是我用来取消前三行注释的内容:
sudo sed -i 's/#\s*location ~ \\\.php$ {/location ~ \\.php$ {/g' /etc/nginx/sites-available/default
sudo sed -i 's/#\s*include snippets\/fastcgi-php\.conf;/\tinclude snippets\/fastcgi-php.conf;/g' /etc/nginx/sites-available/default
sudo sed -i 's/#\s*fastcgi_pass unix:\/run\/php\/php7\.0-fpm\.sock;/\tfastcgi_pass unix:\/run\/php\/php7\.1-fpm.sock;/g' /etc/nginx/sites-available/default
我设法使这些行起作用,但是我不确定如何在不取消注释其余部分的情况下取消注释的注释。关于如何解决这个问题有什么想法吗?
解决方案
多亏温特姆特(Wintermute)的回答,我才能使事情顺利进行。这是我的最终解决方案,全部打包在一个命令行中:
sudo sed -i '/^\s*#\s*location ~ \\\.php\$ {/ {
:loop /\n\s*#\s*}/! {
N;
b loop;
};
s@#@@;
s@#\(\s*include snippets/fastcgi-php\.conf;\)@\1@;
s@#\(\s*fastcgi_pass unix:/run/php/php7\.\)[0-9]\+\(-fpm\.sock;\)@\11\2@;
s@\n\(\s*\)#\(\s*}\)@\n\1\2@;
};' /etc/nginx/sites-available/default
哔哔one
万千封印