Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

linux - Add blank line after every result in grep

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

I would lke to have output as:

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

pipe grep output through

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...