在 grep 中的每个结果后添加空行 [英] Add blank line after every result in grep

查看:20
本文介绍了在 grep 中的每个结果后添加空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 grep 命令看起来像这样zgrep -B bb -A aa "模式" *

my grep command looks like this zgrep -B bb -A aa "pattern" *

我希望输出为:

file1:line1
file1:line2
file1:line3
file1:pattern
file1:line4
file1:line5
file1:line6
            </blank line>
file2:line1
file2:line2
file2:line3
file2:pattern
file2:line4
file2:line5
file2:line6

问题是很难区分第一个找到的结果对应的行何时结束,第二个找到的结果对应的行何时开始.

The problem is that its hard to distinguish when lines corresponding to the first found result end and the lines corresponding to the second found result start.

请注意,尽管 man grep 说在连续匹配组之间添加了--".仅当在同一文件中找到多个匹配项时才有效.但在我的搜索中(如上)我正在搜索多个文件.

Note that although man grep says that "--" is added between contiguous group of matches. It works only when multiple matches are found in the same file. but in my search (as above) I am searching multiple files.

还要注意,在每个 bb+aa+1 行之后添加一个新的空行是行不通的,因为如果文件在模式之前的行少于 bb 行会怎样.

also note that adding a new blank line after every bb+aa+1 line won't work because what if a file has less than bb lines before the pattern.

推荐答案

pipe grep output through

pipe grep output through

awk -F: '{if(f!=$1)print ""; f=$1; print $0;}'

这篇关于在 grep 中的每个结果后添加空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆