特定模式后更换Sed

时间:2014-12-18 12:08:05

标签: regex linux bash sed

鉴于这种模式:

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None //change this one
        Require all granted
</Directory>

<Directory /srv/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
</Directory>

正在运行$ sed -e 's/AllowOverride None/AllowOverride All/i' file会更改AllowOverride

的所有实例

如何更改AllowOverride None

AllowOverride All<Directory /var/www/>

4 个答案:

答案 0 :(得分:3)

通过sed,

$ sed  '/<Directory \/var\/www\/>/,/<\/Directory>/{s~AllowOverride None~AllowOverride All~i}' file
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All //change this one
        Require all granted
</Directory>

<Directory /srv/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
</Directory>

/<Directory \/var\/www\/>/,/<\/Directory>/用于挑选属于某个范围的那些行。 {s~AllowOverride None~AllowOverride All~i}只会在这些匹配的行上进行替换。

答案 1 :(得分:2)

您可以将sed中的地址范围指定为

$ $ sed -e '\#<Directory /var/www/>#, \#</Directory># s/AllowOverride None/AllowOverride All/i' input

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All //change this one
        Require all granted
</Directory>

<Directory /srv/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
</Directory>
  • #<Directory /var/www/>#, \#</Directory>#确保仅针对起始模式Directory /var/www/>和结束模式</Directory>
  • 中的行执行操作

答案 2 :(得分:1)

您可以将awkRS=

一起使用
awk -v RS= -v ORS='\n\n' '/<Directory \/var\/www\/>/ {
            sub(/AllowOverride None/, "AllowOverride All")} 1' file
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All //change this one
        Require all granted
</Directory>

<Directory /srv/>
       Options Indexes FollowSymLinks
       AllowOverride None
       Require all granted
</Directory>

使用RS=会将记录分隔符设置为空字符串,为每个记录提供每个<Directory>...</Directory>块。然后,只需将记录与<Directory /var/www/>匹配,并使用sub进行简单替换。

答案 3 :(得分:0)

sed '/<Directory \/var\/www\/>/,/^<\Directory>.$/s/AllowOverride None/AllowOverride All/g'

跨多行查找模式

<Directory \/var\/www\/>/,/^<\Directory>.$

查找以<Directory /var/www/>开头并以仅</Directory>

的行结束的每一行

然后在每个模式组中用AllowOverride All替换AllowOverride None

<强>输出

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All //change this one
        Require all granted
</Directory>

<Directory /srv/>
       Options Indexes FollowSymLinks
       AllowOverride All
       Require all granted
</Directory>