シェルの2つのラベルの間にあるコメント行

シェルの2つのラベルの間にあるコメント行

インスタンスの2つの構成セクションタグ間でキャッシュを無効にする/有効にするには、ORを使用してhttpdコメントを解除/コメント解除します。sedawk

Cache/キーワードで始まる行にコメントを付けたりコメントを解除したりできますが、Expiresまだモジュールタグにコメントが残っています。開始タグと終了タグの間のすべての行に単一のコマンドでコメントアウト/コメント解除を実行する方法でコメントアウト/コメント解除を実行するにはどうすればよいですか?

これはサンプルキャッシュ構成です。

#<IfModule mod_cache.c>
#
#<IfModule mod_disk_cache.c>
# CacheRoot "/var/cache/mod_proxy"
# CacheEnable disk 
# CacheEnable disk 
# CacheEnable disk 
# CacheIgnoreCacheControl On
# CacheDirLevels 1
#</IfModule>
#
#</IfModule>

#<IfModule mod_expires.c>
#        Header set Cache-Control "max-age=604800, public"
#        ExpiresActive On
#        ExpiresDefault "access plus 1 week"
#        ExpiresByType text/cache-manifest "access plus 0 seconds"
#        ExpiresByType text/html "access plus 1 year"
#        ExpiresByType text/xml "access plus 0 seconds"
#        ExpiresByType application/xml "access plus 0 seconds"
#        ExpiresByType application/json "access plus 0 seconds"
#        ExpiresByType application/rss+xml "access plus 1 hour"
#        ExpiresByType application/atom+xml "access plus 1 hour"
#        ExpiresByType image/x-icon "access plus 1 week"
#        ExpiresByType image/gif "access plus 1 month"
#        ExpiresByType image/png "access plus 1 month"
#        ExpiresByType image/jpg "access plus 1 month"
#        ExpiresByType image/jpeg "access plus 1 month"
#        ExpiresByType video/ogg "access plus 1 month"
#        ExpiresByType audio/ogg "access plus 1 month"
#        ExpiresByType video/mp4 "access plus 1 month"
#        ExpiresByType video/webm "access plus 1 month"
#        ExpiresByType application/x-font-ttf "access plus 1 month"
#        ExpiresByType font/opentype "access plus 1 month"
#        ExpiresByType application/x-font-woff "access plus 1 month"
#        ExpiresByType image/svg+xml "access plus 1 month"
#        ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
#        ExpiresByType text/css "access plus 1 year"
#        ExpiresByType application/javascript "access plus 1 year"
#</IfModule>

<IfModule mod_cache.c>だから私は、タグが閉じるまでタグの開始間のすべての行をコメントアウトできるコマンドが欲しいです<IfModule mod_expires.c>

答え1

この試み、

モジュールにコメントを付けたりコメントを解除したりするには、スクリプトに以下を追加します。

 if [ "$2" == uncomment ]; then
    sed -i "/<IfModule $1>/,/<\/IfModule>/ s/^#//" apache.conf
 elif [ "$2" == comment ]; then
    sed -i "/<IfModule $1>/,/<\/IfModule>/ s/^\(<\| \)/#\1/" apache.conf
 fi

スクリプトを実行するための構文:

sh script.sh <moduleName> <comment/uncomment>

例:

sh script.sh mod_disk_cache.c uncomment

答え2

NETのシンプルなステートマシンを使用してこれを行うことができますawk。簡単にするために、次のようにしましょうmod.xml

#stuff
#
#<IfModule mod_disk_cache.c>
# CacheRoot "/var/cache/mod_proxy"
# CacheEnable disk
# CacheEnable disk
# CacheEnable disk
# CacheIgnoreCacheControl On
# CacheDirLevels 1
#</IfModule>
#
#other stuff

次に、

$ cat mod.xml | awk -F'#' 'BEGIN { state = 0; } { if (/mod_disk_cache/) state = !state; if (state) print $2; else print $0; if (/\/IfModule/) state = !state; }' > umod.xml
$ cat umod.xml
#stuff
#
<IfModule mod_disk_cache.c>
 CacheRoot "/var/cache/mod_proxy"
 CacheEnable disk
 CacheEnable disk
 CacheEnable disk
 CacheIgnoreCacheControl On
 CacheDirLevels 1
</IfModule>
#
#other stuff

この変数は、stateコメント/コメント解除したいブロックの内側(state == 0)または外側()にあるときを記録します。state != 0

メモ:ここで重要な点の1つは、上記のブロックにaで終わるサブブロックが含まれていないことです</IfModule>。これにより、状態が早期にリセットされる可能性があるためです。これを支援する必要がある場合、国はより多くを確保する必要があります。たとえば、-1ターゲットブロックの外側にある場合はステータスを整数に設定し、ターゲットブロックに入るときはステータスを整数に設定し、0サブブロックに入るときはより高い値に増やすことを検討してください。

mod_cache.cこのコマンドは、ブロックにサブブロックがネストされている元の入力を処理します。

$ cat mod.xml | awk -F# 'BEGIN { state = -1; } { if (/<IfModule mod_cache\.c>/) state = 0; else if (/<IfModule .*>/) state++; if (state >= 0) print $2; else print $0; if (/<\/IfModule/) state--;}'

簡単な例に戻り、コメントをもう一度やり直すには、同様の操作を実行できます。出力フィールド区切り記号(OFS)を設定してtrueと評価し#たら、各行の前にaを印刷します。state

$ cat umod.xml | awk -F'#' 'BEGIN { OFS=""; state = 0; } { if (/mod_disk_cache/) state = !state; if (state) print "#", $0; else print $0; if (/\/IfModule/) state = !state; }'
#stuff
#
#<IfModule mod_disk_cache.c>
# CacheRoot "/var/cache/mod_proxy"
# CacheEnable disk
# CacheEnable disk
# CacheEnable disk
# CacheIgnoreCacheControl On
# CacheDirLevels 1
#</IfModule>
#
#other stuff

答え3

コメントを削除するには、次のコマンドを使用できます。

awk '/#.*IfModule mod_cache.c/,/#.*IfModule mod_expires.c/{gsub("#","",$0)}1'  filename

コメント

awk '/^<IfModule mod_cache.c/,/^<IfModule mod_expires.c/{$0="#"$0}1'  filename

関連情報