自動エスケープ文字を持つ sed

自動エスケープ文字を持つ sed
CommentAppend() {
    # Comment line and append line below commented line 
    local comment="$1"      # search this line and comment it 
    local append="$2"       # Append this line below commented line 
    local InputFile="$3"    

    sed -i "s/${comment}/#${comment}/g ; s/#${comment}/& \n${append}/" $InputFile
}

この関数はエスケープされていない文字に対してはうまく機能しますが、エスケープ文字を使用できる場合は失敗します。

では、エスケープ文字の関数を作成できますか?

ベストアンサー1

次のことができます。

CommentAppend() {
    # Comment line and append line below commented line 
    local comment="$1"      # search this line and comment it 
    local append="$2"       # Append this line below commented line 
    local InputFile="$3"    

    perl -pi -e "s/\Q${comment}\E/#${comment}\n${append}/g" "$InputFile"
}

Perl正規表現の区切り文字は、\Q...\Eそれらの間のすべての内容が正規表現ではなくリテラル文字列として解釈されることを保証します(参考文献を参照perldoc perlre)。

"$InputFile"置換は1つのステップでのみ実行でき、単語の区切りを防ぐためにファイル名を引用符で囲む必要があります(例:)。これは、またはを使用してもsed適用されますperl

おすすめ記事