BashスクリプトでPerlを使用して行を削除する

BashスクリプトでPerlを使用して行を削除する

このコマンドは次のとおりです。

sed -i "\|^pref\.fullscreen\.toolbarPixels|d" ~/.vmware/preferences

働く
しかし、このコマンドは:

perl -pi -e "\|^pref\.fullscreen\.toolbarPixels|d" ~/.vmware/preferences

私に出力を与える:

Backslash found where operator expected at -e line 1, near "pref\"
Backslash found where operator expected at -e line 1, near "fullscreen\"
syntax error at -e line 1, near "\|"
Execution of -e aborted due to compilation errors.

なぜ?

修正する:

test.txt ファイルには以下が含まれます。

melon
banana
peach
apple

このコマンドは次のとおりです。

perl -ni -e "print unless m|apple|; print unless m|banana|" test.txt

返品:

melon
melon
banana
peach
peach
apple

変える:

melon
peach

なぜ?

ベストアンサー1

リンゴ/バナナ問題の場合、両方のステートメントはファイルの行で機能するため、重複が発生します。ステートメントフィルタが存在しないか重複melonしていますpeach

次のアプローチを使用すると、重複を防ぎ、必要な削除を達成できます。

$ perl -ni -e "print unless m/(apple|banana)/" test.txt

正規表現で可変性を移動する別の方法は次のとおりです。

$ perl -ni -e "print unless m/apple/ or m/banana/" test.txt

おすすめ記事