Add blank line after every result in grep

前端 未结 7 1982
北荒
北荒 2020-12-14 18:24

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
file         


        
相关标签:
7条回答
  • 2020-12-14 18:50

    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.

    0 讨论(0)
  • 2020-12-14 18:50

    You can also use the --group-separator parameter, with an empty value, so it'd just add a new-line.

    some-stream | grep --group-separator=
    
    0 讨论(0)
  • 2020-12-14 18:58

    pipe grep output through

    awk -F: '{if(f!=$1)print ""; f=$1; print $0;}'
    
    0 讨论(0)
  • 2020-12-14 18:59

    There is no option for this in grep and I don't think there is a way to do it with xargs or tr (I tried), but here is a for loop that will do it (for f in *; do grep -H "1" $f && echo; done):

    [ 11:58 jon@hozbox.com ~/test ]$ for f in *; do grep -H "1" $f && echo; done
    
    a:1
    
    b:1
    
    c:1
    
    d:1
    
    [ 11:58 jon@hozbox.com ~/test ]$ ll
    -rw-r--r--  1 jon  people     2B Nov 25 11:58 a
    -rw-r--r--  1 jon  people     2B Nov 25 11:58 b
    -rw-r--r--  1 jon  people     2B Nov 25 11:58 c
    -rw-r--r--  1 jon  people     2B Nov 25 11:58 d
    

    The -H is to display file names for grep matches. Change the * to your own file glob/path expansion string if necessary.

    0 讨论(0)
  • 2020-12-14 19:07

    I can't test it with the -A and -B parameters so I can't say for sure but you could try using sed G as mentioned here on Unix StackEx. You'll loose coloring though if that's important.

    0 讨论(0)
  • 2020-12-14 19:08

    Try with -c 2; with printing a context I see grep is separating its found o/p

    0 讨论(0)
提交回复
热议问题