亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過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 關注
  • 469 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號