ファイルの文字列を別のファイルに置き換えるには?

ファイルの文字列を別のファイルに置き換えるには?

文字列を含む複数のファイルがあります。文字列を他のファイルの全内容(複数行可能)に置き換える必要があります。どうすればいいですか?

私が必要とするのは、文字列 "filename"ではなく、実際のファイルがsed -i 's/string/filename/' *どこにあるのかなどです。filename

追加情報:ファイルには、/または\または|などの特殊文字を含めることができます。[]

ベストアンサー1

bashはこれに対してうまく機能します。

$ cat replace
foo/bar\baz
the second line

$ cat file
the replacement string goes >>here<<

$ repl=$(<replace)

$ str="here"

$ while IFS= read -r line; do
    echo "${line//$str/$repl}"
done < file
the replacement string goes >>foo/bar\baz
the second line<<

awkは動作します。ただバックスラッシュエスケープを解釈するだけです(\b私の例では)。

$ awk -v string="here" -v replacement="$(<replace)" '
    {gsub(string, replacement); print}
' file
the replacement string goes >>foo/baaz
the second line<<

おすすめ記事