鉴于这种模式:
<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/>
答案 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)
您可以将awk
与RS=
:
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>