¿Cómo puedo hacer que grep imprima las líneas debajo y encima de cada línea coincidente? [duplicar]


Posible Duplicado:
grep un archivo, pero mostrar varias líneas circundantes?

Tengo que analizar un archivo muy grande y quiero usar el comando grep (o cualquier otra herramienta).

Quiero buscar en cada línea de registro la palabra FAILED, luego imprimir la línea arriba y debajo de cada línea coincidente, así como la línea coincidente.

Por ejemplo:

id : 15
Satus : SUCCESS
Message : no problem

id : 15
Satus : FAILED
Message : connection error

Y necesito imprimir:

id : 15
Satus : FAILED
Message : connection error
 267
Author: Community, 2009-07-02

3 answers

La opción de Grep -A 1 le dará una línea después; -B 1 le dará una línea antes; y -C 1 combina ambas para darle una línea tanto antes como después.

 546
Author: pgs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-30 19:07:27

Use la opción-B y-A

grep --help
...
-B, --before-context=NUM  print NUM lines of leading context
-A, --after-context=NUM   print NUM lines of trailing context
...
 36
Author: zinovii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-02 05:29:00

Use los interruptores-A y-B (líneas medias-después y líneas-antes):

grep -A 1 -B 1 FAILED file.txt
 32
Author: Milan Babuškov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-02 05:28:22