2017-03-14 20 views
0

私は、httpd.confの次のブロックのようなものを検索し、AllowOverrideに "None"を "None"に置き換えるPerlコード行をMakefileに追加します。Perl複数行正規表現の置き換えキャプチャグループ

<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.4/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 

    # 
    # Controls who can get stuff from this server. 
    # 
    Require all granted 
</Directory> 

次のように私は、コマンドラインから実行しようとしているコードは次のとおりです。

sudo perl -p -i -e 's/(<Directory "\/var\/www\/html">.*AllowOverride)(None)/\1 All/' httpd.conf 

しかし、私はそれを動作させることはできません。私は最初のグループを同じに保ち、2番目のグループを置き換えるために2つのキャプチャグループを使用しています。

ご協力いただきまして誠にありがとうございます。

EDITは:これは急速に起こりやすいエラーを複雑になる正規表現で入れ子になったものを解析し、修正し、一般的に

sudo perl -0777 -p -i -e 's/(<Directory \"\/var\/www\/html\">.*?AllowOverride) (None)/\1 All/s' httpd.conf 
+1

'-p'フラグは、デフォルトでは一度に1行だけを読み込みます。 '-0777'を追加することで、一度に複数の行を削除してみてください。 Perl RegExで複数の任意の文字(改行を含む)を置き換える方法も参照してください。(http://stackoverflow.com/q/36533282/2173773) –

+1

また、貪欲でない '。*?'さもなければ、最後の 'AllowOverride'まで完全に一致します。 '\ 1'または' \ K'(正のlookbehind_の形式)ではなく '$ 1'を使います。 – zdim

+0

sudo perl -0777 -p -i -e '/(<ディレクトリ\ "\/var \/www \/html \">。*?AllowOverride)(なし)/ \ 1すべて/ httpd.conf – lorenzo

答えて

3

を解決しました。可能であれば、完全なパーサを使用してください。

幸運なことに、Apacheの設定ファイルであるApache::Admin::Configを読んで変更する方法があります。最初はちょっと変わったので、ここに例があります。

#!/usr/bin/env perl 

use strict; 
use warnings; 
use v5.10; 

use Apache::Admin::Config; 

# Load and parse the config file. 
my $config = Apache::Admin::Config->new(shift) 
    or die $Apache::Admin::Config::ERROR; 

# Find the <Directory "/var/www/html"> section 
# NOTE: This is a literal match, /var/www/html is different from "/var/www/html". 
my $section = $config->section(
    "Directory", 
    -value => q["/var/www/html"] 
); 

# Find the AllowOverride directive inside that section. 
my $directive = $section->directive("AllowOverride"); 

# Change it to All. 
$directive->set_value("All"); 

# Save your changes. 
$config->save; 

一度に1レベルずつ構造をドリルダウンしています。最初にセクションを見つけ、その中のディレクティブを探します。

これはループで実行できます。たとえば、すべてのディレクトリセクションを見つける...

for my $section ($config->section("Directory")) { 
    ... 
} 
関連する問題