あるファイルの文字列を別のファイルの内容全体に置き換えるsed(またはawk)コマンドを作成したいと思います。

あるファイルの文字列を別のファイルの内容全体に置き換えるsed(またはawk)コマンドを作成したいと思います。

あるファイルの文字列を別のファイルの内容全体に置き換えるsed(またはawk)コマンドを作成したいと思います。コンテンツをインポートする2番目のファイルには1行以上があります。

$ cat file.txt 
TEXT1 
TEST2 
TEST3 

そして

$ cat other_file.txt 
there are multiple lines1 
there are multiple lines2 
there are multiple lines3 

私は出力が次のようになります:

$ cat file3.txt
there are TEXT1 
TEST2 
TEST3 lines1

there are TEXT1 
TEST2 
TEST3 lines2

there are TEXT1 
TEST2 
TEST3 lines3

私はこれを試しました:

sed -i -e '/PLACEHOLDER/ r file' -e s/PLACEHOLDER// otherFile

しかし、それは私に正しい出力を提供しません。

「複数」キーワードをファイルの各行のすべての項目に置き換えます。これが明確になることを願っています。

ベストアンサー1

m4マクロプロセッサを使用して、PLACEHOLDER他のファイルを含むマクロを定義します。

$ cat file
foo PLACEHOLDER baz
$ cat otherfile
These are the
multi-line contents
of the other file.
$ m4 -D PLACEHOLDER='include(otherfile)' file
foo These are the
multi-line contents
of the other file.
 baz

otherfileこれには、最後の改行文字を削除しないままのテキストが含まれます。

おすすめ記事