find -execのsedコマンドでリテラル空の中括弧{}を使用してください。

find -execのsedコマンドでリテラル空の中括弧{}を使用してください。

{}これを行うことができるかどうか疑問に思います。sedfind -exec

一例:

find "$dir" -type f -name "*" -exec sed -i s/hello{}a/hello{}b/g '{}' +

重複したエラーメッセージが表示されます{}

find: Only one instance of {} is supported with -exec ... +

見つかったファイルを置き換えるのではなく、コマンド{}に残ってリテラルとして処理される方法はありますか?sedfind

ベストアンサー1

この場合、中find括弧式をキャプチャし、代替テキストの逆参照を使用してexec構文を解決できます。

$ cat f1 f2
f1: hello{}a
f2: hello{}a
$ find . -type f -exec sed -i 's/hello\([{][}]\)a/hello\1b/g' '{}' +
$ cat f1 f2
f1: hello{}b
f2: hello{}b

またはもっと簡単です(説明で述べたように)。

find "$dir" -type f -exec sed -i 's/\(hello[{]}\)a/\1b/g' {} +

Sedのオプションは-i移植性がなく、どこでも機能しません。指定されたコマンドはGNU Sedでのみ機能します。

詳細については、次を参照してください。

おすすめ記事