Add Blank Line After Every Result in Grep

How to add blank line after every grep result using Perl?

This is the direct answer to your question:

 grep 'xyz' | perl -pe 's/$/\n/'

But this is better:

 perl -ne 'print "$_\n" if /xyz/'

EDIT

Ok, after your edit, you want (almost) this:

grep 'xyz' * | perl -pe 'print "\n" if /^([^:]+):/ && ! $seen{$1}++'

If you don’t like the blank line at the beginning, make it:

grep 'xyz' * | perl -pe 'print "\n" if /^([^:]+):/ && ! $seen{$1}++ && $. > 1'

NOTE: This won’t work right on filenames with colons in them. :)½

Adding blank line spaces before and after pattern 'string' match


sed -r 's/(^.*)(string)(.*$)/\1\n\n\n\n\n\2\n\n\n\n\n\3/' text.txt

Use -r or -E to allow regular expressions, split likes into three sections and then substitute the line for the first section, 5 new lines, the second section, 5 new lines and then finally the third section.

Add blank line before a certain phrase in a text file in Linux?

Sure, you can use Perl.

perl -pe 's/^Nmap/\nNmap/'


Related Topics



Leave a reply



Submit