パターンが最初に発生した後に別のファイルにファイルを挿入する

パターンが最初に発生した後に別のファイルにファイルを挿入する

一致するPATTERNの後にfile1の内容をfile2に挿入したいと思います。パターンが最初に表示された後にのみこれを実行したいと思います。

私のニーズに応じて、次のコマンドにどのような修正が必要かを知りたいです。

sed -i "/PATTERN/r file1" file2

ベストアンサー1

sed '/PATTERN/{
       r file1
       :a
       n
       ba
     }' file2

:a、、、nbaPATTERNから最後までファイル全体の内容を出力するループです。この6行は1つのコマンドにすぎませんが、区切るには改行が必要で、r次の:sedコマンドが必要ですb

追加情報info sed:

`n'
     If auto-print is not disabled, print the pattern space, then,
     regardless, replace the pattern space with the next line of input.
     If there is no more input then `sed' exits without processing any
     more commands.

`: LABEL'
     [No addresses allowed.]

     Specify the location of LABEL for branch commands.  In all other
     respects, a no-op.

`b LABEL'
     Unconditionally branch to LABEL.  The LABEL may be omitted, in
     which case the next cycle is started.

おすすめ記事