next up previous contents index PLPL moodlepserratamodulosperlmonksperldocapuntes LHPgoogleetsiiullpcgull
Sig: Suprimir: El Comando D Sup: Expresiones Regulares en sed Ant: Del espacio de Patrones Err: Si hallas una errata ...

La orden N

N añade la siguiente línea de entrada al espacio de patrones. Las dos líneas se separan mediante un retorno de carro. Después de su ejecución, el comando N pasa el control a los subsiguientes comandos en el script.
El siguiente ejemplo propuesto en [1] muestra una búsqueda y sustitución multilínea. Se trata de sustituir la cadena ``Owner and Operator Guide'' por ``Installation Guide'', cualquiera que sea su posición en una línea o entre ellas. Los autores de [1] y [2] proponen la siguiente solución:
$ 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.
Consideremos esta otra solución:
$ 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 $ \backslash$ . 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.


next up previous contents index PLPL moodlepserratamodulosperlmonksperldocapuntes LHPgoogleetsiiullpcgull
Sig: Suprimir: El Comando D Sup: Expresiones Regulares en sed Ant: Del espacio de Patrones Err: Si hallas una errata ...
Casiano Rodríguez León
2012-05-22