
Tengo dos archivos:
Este es el contenido del archivo 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 es el contenido del archivo 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
Quiero la salida del archivo 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
Quiero imprimir el contenido del archivo A entre el patrón 2 y el patrón 3 del archivo B.
Actualmente estoy usando esto:
awk '/pattern-2/{c++;if(c==2){printf $0; print "\n"; while(getline line<"file-A"){print line};next}}1' file-B
y está funcionando bien, pero necesito algo que busque tanto el patrón como luego coloque otro contenido de archivo entre ellos.
Respuesta1
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
O si usa patrones de expresiones regulares en lugar de cadenas, use algo como
last ~ /pattern-2/ && /pattern-3/{
en la segunda línea.