Sed nos permite de forma muy sencilla sacar de un fichero de texto el contenido entre dos palabras o strings, ambas inclusive. Por supuesto nos podemos aprovechar del uso de expresiones regulares y todo lo que sed permite para crear búsquedas más complejas.
La sintaxis es la siguiente. La «,» es un operador de rangos:
$ sed -n ‘/INICIO/,/FIN/p’ fichero
Nota: utilizamos el parámetro -n para evitar visualizar las líneas que coinciden con el patrón duplicadas
Por ejemplo, con el contenido siguiente en sed.txt:
If no -e, --expression, -f, or --file option is given, then
the first non-option argument is taken as the sed script to
interpret. All remaining arguments are names of input
files; if no input files are specified, then the standard
input is read.
GNU sed home page: .
General help using GNU software: . E-mail bug reports to: . Be
sure to include the word ``sed'' somewhere in the ``Sub‐
ject:'' field.
Sacamos lo que hay entre las líneas que contienen GNU y field:
$ sed -n '/GNU/,/field/p' sed.txt
GNU sed home page: .
General help using GNU software: . E-mail bug reports to: . Be
sure to include the word ``sed'' somewhere in the ``Sub‐
ject:'' field.