Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 213
maverickprac Avatar asked Nov 25 '11 19:11

maverickprac


People also ask

How can I get line after grep?

To also show you the lines before your matches, you can add -B to your grep. The -B 4 tells grep to also show the 4 lines before the match. Alternatively, to show the log lines that match after the keyword, use the -A parameter. In this example, it will tell grep to also show the 2 lines after the match.

How do I grep blank lines in a file?

To match empty lines, use the pattern ' ^$ '. To match blank lines, use the pattern ' ^[[:blank:]]*$ '. To match no lines at all, use the command ' grep -f /dev/null '.

How do you grep multiple lines after a match?

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match. If you want the same number of lines before and after you can use -C num . This will show 3 lines before and 3 lines after.

What does \b do in grep?

The ' \ ' character, when followed by certain ordinary characters, takes a special meaning: ' \b ' Match the empty string at the edge of a word.


3 Answers

pipe grep output through

awk -F: '{if(f!=$1)print ""; f=$1; print $0;}'
like image 154
Michael Krelin - hacker Avatar answered Oct 22 '22 08:10

Michael Krelin - hacker


Pipe | any output to:

sed G

Example:

ls | sed G

If you man sed you will see

G Append's a newline character followed by the contents of the hold space to the pattern space.

like image 18
jasonleonhard Avatar answered Oct 22 '22 06:10

jasonleonhard


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.

If you don't mind a -- in lieu of a </blank line>, add the -0 parameter to your grep/zgrep command. This should allow for the -- to appear even when searching multiple files. You can still use the -A and -B flags as desired.

like image 4
YenForYang Avatar answered Oct 22 '22 08:10

YenForYang