
2 つのファイルがあります:
ファイル A の内容は次のとおりです。
etc...
this is a test file having \@ and \# and \$ as well
looking for awk or sed solution to print this content in another file between the matching pattern
etc....
ファイル B の内容は次のとおりです。
file-B begin
this is a large file containing many patterns like
pattern-1
pattern-2
pattern-2
pattern-3
pattern-2
pattern-4
file-B end
ファイル B の出力は次のようになります。
file-B begin
this is a large file containing many patterns like
pattern-1
pattern-2
pattern-2
etc...
this is a test file having \@ and \# and \$ as well
looking for awk or sed solution to print this content in another file between the matching pattern
etc....
pattern-3
pattern-2
pattern-4
file-B end
ファイル B のパターン 2 とパターン 3 の間にあるファイル A の内容を印刷します。
現在、私はこれを使用しています:
awk '/pattern-2/{c++;if(c==2){printf $0; print "\n"; while(getline line<"file-A"){print line};next}}1' file-B
正常に動作していますが、両方のパターンを検索し、その間に別のファイル コンテンツを配置するものが必要です。
答え1
awk -v file="file-A" '
last=="pattern-2" && $0=="pattern-3"{
while ((getline line < file)>0) print line
close(file)
}
{last=$0}1
' file-B
または、文字列の代わりに正規表現パターンを使用する場合は、次のようにします。
last ~ /pattern-2/ && /pattern-3/{
2行目にあります。