Linux

sed + 刪除“#”字元以防萬一出現行

  • October 29, 2017

我們有以下文件:

  cat graphite-web.conf

  #<IfModule mod_authz_core.c>
  #    # Apache 2.4
  #    Require local
  #    Order Allow,Deny
  #    Allow from All
  #    Require all granted
  #</IfModule>
  #<IfModule !mod_authz_core.c>
  #    # Apache 2.2
  #    Require all granted
  #    Order Allow,Deny
  #    Deny from all
  #    Allow from All
  #    Allow from ::1
  #</IfModule>

什麼是在“要求所有已授予”行之前刪除 # 的最佳方法

  • “#”字元不在行首

預期輸出:

  #<IfModule mod_authz_core.c>
  #    # Apache 2.4
  #    Require local
  #    Order Allow,Deny
  #    Allow from All
       Require all granted
  #</IfModule>
  #<IfModule !mod_authz_core.c>
  #    # Apache 2.2
  #    Require all granted
  #    Order Allow,Deny
  #    Deny from all
  #    Allow from All
  #    Allow from ::1
  #</IfModule>

sed解決方案:

sed 's/#\([[:space:]]*Require all granted\)/ \1/' graphite-web.conf

輸出:

  #<IfModule mod_authz_core.c>
  #    # Apache 2.4
  #    Require local
  #    Order Allow,Deny
  #    Allow from All
       Require all granted
  #</IfModule>
  #<IfModule !mod_authz_core.c>
  #    # Apache 2.2
       Require all granted
  #    Order Allow,Deny
  #    Deny from all
  #    Allow from All
  #    Allow from ::1
  #</IfModule>

要就地編輯文件 - 添加-i選項:

sed -i ....

引用自:https://unix.stackexchange.com/questions/401276