Como adicionar o conteúdo do arquivo A ao arquivo B entre a correspondência de padrão

Como adicionar o conteúdo do arquivo A ao arquivo B entre a correspondência de padrão

Eu tenho dois arquivos:

Este é o conteúdo do arquivo 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....

Este é o conteúdo do arquivo 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 

Quero a saída do arquivo B como:

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 

Quero imprimir o conteúdo do arquivo A entre o padrão 2 e o padrão 3 do arquivo B.

Atualmente estou usando isso:

awk '/pattern-2/{c++;if(c==2){printf $0; print "\n"; while(getline line<"file-A"){print line};next}}1' file-B

e está funcionando bem, mas preciso de algo que pesquise o padrão e coloque outro conteúdo de arquivo entre eles.

Responder1

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

Ou se você usar padrões regex em vez de strings, use algo como

  last ~ /pattern-2/ && /pattern-3/{

na segunda linha.

informação relacionada