1

Below is file output that I want to access

[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)

I want output if I 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)

How to use grep command to show below output?

3

You could use grep with -A. Something like:

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

The -A specifies the number of lines you want to include after the match.

But I think it would be better to use sed in this case:

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

This will print everything between [2] and an empty line.

The same using awk:

$ awk -v RS='' -v ORS='\n\n' '/^\[2\]/' inputfile.txt
  • The thing I missed most when moving away from AIX was the grep -p option. Outputs the paragraph which includes the match (and you can tell it what paragraph separator to use). Someone should add that to GNU grep. – EightBitTony Jan 18 '16 at 9:11
  • 1
    There is already an open feature request for that: savannah.gnu.org/bugs/?14630 – zuazo Jan 18 '16 at 9:23
  • And what to do if I want to grep Total file size: after [2] and before [3] means between [2] and [3] – LOKESH Jan 18 '16 at 9:42
  • There are multiple ways to achieve this with sed. The simplest way based on my example: sed -n '/^\[2\]/,/^$/{/^Total file size:/p}' inputfile.txt – zuazo Jan 18 '16 at 10:01
  • Your sed commands output show me [2] and [3] both's output – LOKESH Jan 19 '16 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.