2017-03-15 44 views
0

我想在第一次使用bash顯示此模式時插入一行。我該怎麼去做呢?我想嘗試使用模式來匹配位置/ {*}如何在跨多行的模式之後插入一行文件

location/{ 
      # First attempt to serve request as file, then 
      # as directory, then fall back to displaying a 404. 
      try_files $uri $uri/ =404; 
      # Uncomment to enable naxsi on this location 
      # include /etc/nginx/naxsi.rules 
    } 

,我希望它成爲

location/{ 
      # First attempt to serve request as file, then 
      # as directory, then fall back to displaying a 404. 
      try_files $uri $uri/ =404; 
      # Uncomment to enable naxsi on this location 
      # include /etc/nginx/naxsi.rules 
    } 
    location /hbnb_static 

謝謝,我會很感激的幫助。

回答

0

我嘗試了Perl的一行程序:

perl -e '$s.=$_ while <>; $s=~s|location/\{.*?\}|$&\n location /hbnb_static|s; print $s' inputfile 

一些說明:

  • 啜食的inputfile成可變$s
  • s正則表達式改性劑使得.匹配換行符以及
  • .*?很懶,所以在遇到後續的時會停止
  • 我假設你想在插入的行前插入4個空格縮進。
  • 打印結果到stdout

使成一個bash腳本讀取STDIN或在命令行上的文件這一點,並打印修改後的文本到stdout:

#!/bin/bash 
perl -e '$s.=$_ while <>; $s=~s|location/\{.*?\}|$&\n location /hbnb_static|s; print $s' $1 
+0

纔有可能要在bash中做到這一點? –

+0

Perl單線程是bash腳本的一行... – SzG

相關問題