2015-12-19 75 views
0

我是sed的新手,並且想要對httpd.conf文件進行修改。SED正則表達式錯誤

這裏是搜索

<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 --- Want to change this to AllowOverride All 

# 
# Controls who can get stuff from this server. 
# 
    Order allow,deny 
    Allow from all 

</Directory> 

任務文本:我的任務是改變AllowOverride NoneAllowOverride All

sed 'N;s:<Directory "/var/www/html">\(.*\)AllowOverride None\(.*\): <Directory "/var/www/html">\1AllowOverride All\2:' /etc/httpd/conf/httpd.conf > newHttpdConf.ini 

正則表達式的說明

開始:< Directory "/var/www/html">

店在任何東西的AllowOverride無:\(.*\)

查找:AllowOverride None

店東西后:\(.*\)

當我在sed命令上面運行時,我沒有看到任何替換。 我正在犯什麼錯誤。

感謝

回答

3

您可以使用正則表達式的範圍來選擇線的一個子集,然後應用sed命令他們

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

它能做什麼?

  • /<Directory "\/var\/www\/html">/, /<\/Directory>/這指定行範圍。這是從/<Directory "\/var\/www\/html">/開始匹配行結束的/<\/Directory>/

  • s/AllowOverride None/AllowOverride All/是否在範圍內的所有AllowOverride None匹配替換匹配。

+0

不錯。我會試試這個。你能告訴我,我的正則表達式有什麼問題,我不會重複同樣的錯誤。 – voila

+1

@voila東西是sed讀行是行和做模式匹配。因此,當它到達行時,它不知道'AllowOverride'是否會出現在後面的行中。而'。*'只會選擇直到行尾而不是下一行 – nu11p01n73R

+0

明白了..感謝您的幫助 – voila