grep content entre dois colchetes iniciais

1

Abaixo está a saída do arquivo que eu quero acessar

[1]
RsyncCommand: 0
Number of files: 18
Number of files transferred: 0
Total file size: 24.01M bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 354
File list generation time: 0.002 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 424
Total bytes received: 69
total size is 24.01M  speedup is 48701.73 (DRY RUN)


[2]
RsyncCommand: 0
Number of files: 21
Number of files transferred: 0
Total file size: 5.22M bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 507
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 586
Total bytes received: 78
total size is 5.22M  speedup is 7862.54 (DRY RUN)


[3]
RsyncCommand: 0
Number of files: 54
Number of files transferred: 0
Total file size: 63.67M bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 1.56K
File list generation time: 0.002 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 1.75K
Total bytes received: 185
total size is 63.67M  speedup is 32988.24 (DRY RUN)

Eu quero saída se eu grep 2

[2]
RsyncCommand: 0
Number of files: 21
Number of files transferred: 0
Total file size: 5.22M bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 507
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 586
Total bytes received: 78
total size is 5.22M  speedup is 7862.54 (DRY RUN)

Como usar o comando grep para mostrar a saída abaixo?

    
por LOKESH 18.01.2016 / 09:50

1 resposta

3

Você pode usar o grep com -A . Algo como:

$ grep -A 13 '^\[2\]' inputfile.txt

O -A especifica o número de linhas que você deseja incluir após a correspondência.

Mas acho que seria melhor usar sed neste caso:

$ sed -n '/^\[2\]/,/^$/p' inputfile.txt

Isso imprimirá tudo entre [2] e uma linha vazia.

O mesmo usando awk :

$ awk -v RS='' -v ORS='\n\n' '/^\[2\]/' inputfile.txt
    
por 18.01.2016 / 09:57