$ cat multiline2.sed #Assume the pattern is in no more than two lines s/Owner and Operator Guide/Installation Guide/g /Owner/{ N s/ *\n/ /g s/Owner *and *Operator *Guide/Installation Guide/g }Veamos primero los contenidos del fichero de prueba:
$ cat multiline.test Dear Owner: Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available for the Owner of your system. Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available on your system. Look in the Owner and Operator Guide, we mean the Owner and Operator Guide shipped with your system. Two manuals are provided including the Owner and Operator Guide and the User Guide. The Owner and Operator Guide is shipped with your system. Look in the Owner and Operator Guide shipped with your system. The Owner and Operator Guide is shipped with your system.La ejecución del script da la siguiente salida:
$ sed -f multiline2.sed multiline.test Dear Owner: Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available for the Owner of your system. Consult Section 3.1 in the Installation Guide for a description of the tape drives available on your system. Look in the Installation Guide, we mean the Installation Guide shipped with your system. Two manuals are provided including the Installation Guide and the User Guide. The Installation Guide is shipped with your system. Look in the Installation Guide shipped with your system. The Owner and Operator Guide is shipped with your system.Uno de los problemas, que aparece en el primer párrafo del ejemplo de prueba, es que la segunda línea leída debe ser "reciclada" para su uso en la siguiente búsqueda. El segundo fallo, que aparece en el último párrafo, es consecuencia de la limitación del script para trabajar con patrones partidos en más de dos líneas.
$ cat multiline.sed s/Owner and Operator Guide/Installation Guide/g /Owner/{ :label N s/\n/ /g s/Owner *and *Operator *Guide/Installation Guide/g /Owner *$/b label /Owner *and *$/b label /Owner *and *Operator *$/b label }Este otro script hace que sed permanezca en un bucle mientras la línea adjuntada en segundo lugar contenga un prefijo estricto de la cadena buscada.
$sed -f multiline.sed multiline.test Dear Owner: Consult Section 3.1 in the Installation Guide for \ a description of the tape drives available for the Owner of \ your system. Consult Section 3.1 in the Installation Guide for a description of the tape drives available on your system. Look in the Installation Guide, we mean the Installation Guide \ shipped with your system. Two manuals are provided including the Installation Guide and the User Guide. The Installation Guide is shipped with your system. Look in the Installation Guide shipped with your system. The Installation Guide is shipped with your system.Un problema que aparece con esta aproximación es la presencia de líneas muy largas. Las líneas permanecen en el espacio de trabajo mientras terminen en un prefijo de la cadena buscada. Para que la salida quepa en la hoja he tenido que partir las líneas del fichero de salida, lo que he indicado con los símbolos . Considere esta modificación:
#!/bin/sed -f s/Owner and Operator Guide/Installation Guide/g /Owner/{ :label N s/Owner\([ \n]*\)and\([ \n]*\)Operator\([ \n]*\)Guide/Installation\1\2Guide\3/g /Owner *$/b label /Owner *and *$/b label /Owner *and *Operator *$/b label }Es indudable la ventaja de disponer de esta capacidad de búsqueda multilínea no puede realizarse con otras utilidades como ex o vi.