あるファイルの内容を別のファイルに挿入してパターン間を置き換える

あるファイルの内容を別のファイルに挿入してパターン間を置き換える

ファイル1.html:

<!doctype html>
<html>
<head>
</head>
<body>
    text
    <!-- start-replacing -->
<p>more text1</p>
<p>more text2</p>
    <!-- end-replacing -->
    other text
</body>
</html>

そしてファイル2.txt

<p>some text</p>
<div>some other text</div>

今私は間のすべてを置き換えるコマンドを探しています。

<!-- start-replacing -->そして<!-- end-replacing -->

file2.txtの内容として

これ出力.htmlしなければならない:

<!doctype html>
<html>
<head>
</head>
<body>
    text
    <!-- start-replacing -->
<p>some text</p>
<div>some other text</div>
    <!-- end-replacing -->
    other text
</body>
</html>

ベストアンサー1

そしてperl

perl -0777 -pe '
  BEGIN{$repl = <STDIN>}
  s/<!-- start-replacing -->\K.*?(?=<!-- end-replacing -->)/$repl/sg
' file1.html < file2.txt > output.html

おすすめ記事