2013-02-08 117 views
0

場景:嘗試使用sed修改大部分httpd配置文件。sed替換爲大文本

我已經試過什麼:

read -d '' _OLD <<"EOF" 
<Directory "/var/www/html"> 

# 
# Possible values for the Options directive are "None", "All", 
# or any combination of: 
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 
# 
# Note that "MultiViews" must be named *explicitly* --- "Options All" 
# doesn't give it to you. 
# 
# The Options directive is both complicated and important. Please see 
# http://httpd.apache.org/docs/2.2/mod/core.html#options 
# for more information. 
# 
    Options Indexes FollowSymLinks 

# 
# AllowOverride controls what directives may be placed in .htaccess files. 
# It can be "All", "None", or any combination of the keywords: 
# Options FileInfo AuthConfig Limit 
# 
    AllowOverride None 
EOF 

read -d '' _NEW <<"EOF" 
<Directory "/var/www/html"> 

# 
# Possible values for the Options directive are "None", "All", 
# or any combination of: 
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 
# 
# Note that "MultiViews" must be named *explicitly* --- "Options All" 
# doesn't give it to you. 
# 
# The Options directive is both complicated and important. Please see 
# http://httpd.apache.org/docs/2.2/mod/core.html#options 
# for more information. 
# 
    Options Indexes FollowSymLinks 

# 
# AllowOverride controls what directives may be placed in .htaccess files. 
# It can be "All", "None", or any combination of the keywords: 
# Options FileInfo AuthConfig Limit 
# 
    AllowOverride All 
EOF 

echo -n ${_OLD} 
echo -n ${_NEW} 
sed -i "s/$_OLD/$_NEW/g" /etc/httpd/conf/httpd.conf 

如果您發現唯一改變的事情就是AllowOverride All我只是想修改的規則/var/www/html而不是其他實例因此,爲什麼我期待在這整個街區。

我知道有可能是一個更好的方式做什麼,我試圖完成。方向將不勝感激。

回答

2
sed -r '\%<Directory "/var/www/html">%,\%</Directory>% s%(AllowOverride)\s+None%\1 All%i' 
1

好了,我的(外的日期)版本的sed不允許多行「舊」模式(正則表達式),需要多行更換新行之前有反斜槓,

sed "s/lifetime/birth\ 
…\ 
death/" 

......所以這是一個或兩個問題。另一種方法是,即使您的模式和替換項在其中有斜線,您仍然使用/作爲分隔符。

我建議你使用正則表達式地址:

sed '/<Directory "\/var\/www\/html">/,/AllowOverride/s/AllowOverride None/AllowOverride All/' 

其他 的話,做任何適用線(S)的s/AllowOverride None/AllowOverride All/替代一個<Directory "/var/www/html">線 和下一個AllowOverride之間line。 (當心在第一正則表達式的報價。)

+0

如果設置AllowOverride未在/ var/www/html等規則出現,這種表達可能會改變以後的規則。 –

+0

@Stephen:好點。但是,OP似乎現在知道文件中的內容。這似乎並沒有工作,要麼和糾正我,如果我用這個錯誤'sed的「/ <目錄 「\ /無功\/WWW \/HTML」> /,/的AllowOverride/S /設置AllowOverride無/的AllowOverride – Scott

+0

@Scott所有/'/ etc/httpd/conf/httpd.conf'實際上輸出整個文件,不改變文件的實際文本。 – Peter