为了账号安全,请及时绑定邮箱和手机立即绑定

通过sed取消注释结束赞誉

通过sed取消注释结束赞誉

犯罪嫌疑人X 2021-04-10 21:18:24
我想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/defaultsudo sed -i 's/#\s*include snippets\/fastcgi-php\.conf;/\tinclude snippets\/fastcgi-php.conf;/g' /etc/nginx/sites-available/defaultsudo 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
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

由于配置文件可能在这一节中包含更多内容,因此在这里要考虑错误匹配。特别是,匹配^#\s*}并希望达到最佳效果可能会导致注释掉文件中其他位置完全不相关的行。


因此,在取消注释之前,我将收集属于该部分的所有行。我正在考虑这样的思路:编写代码


/^#\s*location ~ \\\.php\$ {/ {

  :loop

  /\n#\s*}/! {

    N

    b loop

  }

  s/^#//

  s@#\(\s*include snippets/fastcgi-php\.conf;\)@\1@

  s@#\(\s*fastcgi_pass unix:/var/run/php/php7\.\)0\(-fpm\.sock;\)@\11\2@

  s/\n#\(\s*}\)/\n\1/

}

进入文件uncomment.sed,然后说,然后运行


sed -f uncomment.sed /etc/nginx/sites-available/default

如果结果令人满意,请添加该-i选项以进行编辑。


该代码的工作方式如下:


/^#\s*location ~ \\\.php\$ {/ {    # starting with the first line of the section

                                   # (regex taken from the question):

  :loop                            # jump label for looping

  /\n#\s*}/! {                     # Until the last line is in the pattern space

    N                              # fetch the next line from input, append it

    b loop                         # then loop

  }


  # At this point, we have the whole section in the pattern space.

  # Time to remove the comments.


  # There's a # at the beginning of the pattern space; remove it. This

  # uncomments the first line.

  s/^#//


  # The middle two are taken from the question, except I'm using @ as 

  # a separator so I don't have to escape all those slashes and captures

  # to avoid repeating myself.

  s@#\(\s*include snippets/fastcgi-php\.conf;\)@\1@

  s@#\(\s*fastcgi_pass unix:/var/run/php/php7\.\)0\(-fpm\.sock;\)@\11\2@


  # Uncomment the last line. Note that we can't use ^ here because that

  # refers to the start of the pattern space. However, because of the

  # looping construct above, we know there's only one # directly after

  # a newline followed by \s*} in it, and that's the comment sign before

  # the last line. So remove that, and we're done.

  s/\n#\(\s*}\)/\n\1/

}


查看完整回答
反对 回复 2021-04-16
?
万千封印

TA贡献1891条经验 获得超3个赞

这是一个gnu-awk带有自定义选项的替代解决方案RS:


awk -v RS='# location [^{]*{[^}]*}\n' 'RT {

   RT = gensub(/(^|\n)[[:blank:]]*#([[:blank:]]*(location|include|fastcgi_pass unix|}))/, "\\1\\2", "g", RT)

   RT = gensub(/(php7\.)0/, "\\11", "1", RT)

}

{ORS=RT} 1' file

...

 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:/var/run/php/php7.1-fpm.sock;

 }

...


查看完整回答
反对 回复 2021-04-16
  • 2 回答
  • 0 关注
  • 442 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信