grep 两个起始括号之间的内容

grep 两个起始括号之间的内容

以下是我要访问的文件输出

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

如果我 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)

如何使用 grep 命令显示以下输出?

答案1

您可以将 grep 与-A.就像是:

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

指定-A匹配后要包含的行数。

sed但我认为在这种情况下使用会更好:

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

这将打印[2]和 空行之间的所有内容。

同样使用awk

$ awk -v RS='' -v ORS='\n\n' '/^\[2\]/' inputfile.txt

相关内容