Home > OS >  Formatting grep output with blank line between two occurrences
Formatting grep output with blank line between two occurrences

Time:11-15

I'm looking to format my output this way, but with appropriate, more graceful code.

In file:

line with foo occurence
line with foo occurence
line with bar occurence
line with bar occurence
line with foo occurence
line with foo occurence

My bash command:

cat file | grep -e foo && printf '\n' && cat file | grep -e bar

And result:

line with foo occurence
line with foo occurence
line with foo occurence
line with foo occurence

line with bar occurence
line with bar occurence

CodePudding user response:

You can try this paste command with grep

paste -zd'\n' <(grep 'foo' input_file) <(grep 'bar' input_file)
line with foo occurence
line with foo occurence
line with foo occurence
line with foo occurence

line with bar occurence
line with bar occurence

CodePudding user response:

How about running the grep command in a loop:

for term in foo bar
do
    grep $term file
    echo  # Add a blank line
done

The above as a one-liner:

for term in foo bar; do grep $term file; echo; done
  • Related