How do I add newlines between lines printed on the command line?

60,792

Solution 1

sed G 
# option: g G    Copy/append hold space to pattern space.

G is not often used, but is nice for this purpose. sed maintains two buffer spaces: the “pattern space” and the “hold space”. The lines processed by sed usually flow through the pattern space as various commands operate on its contents (s///, p, etc.); the hold space starts out empty and is only used by some commands.

The G command appends a newline and the contents of the hold space to the pattern space. The above sed program never puts anything in the hold space, so G effectively appends just a newline to every line that is processed.

Solution 2

Use awk to add an extra newline. This also lets you filter out things you don't want.

awk '{print $0,"\n"}' | less

Solution 3

Use sed and replace the whole line by itself plus one extra newline character:

grep foo /var/log/maillog | sed -e "s/^.*$/&1\n/"

Solution 4

Is this what you are after?

grep SPAM mail.log | while read -r line; do echo; echo $line; done

Solution 5

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.

Share:
60,792

Related videos on Youtube

xenoterracide
Author by

xenoterracide

Former Linux System Administrator, now full time Java Software Engineer.

Updated on September 17, 2022

Comments

  • xenoterracide
    xenoterracide almost 2 years

    Mail logs are incredibly difficult to read. How could I output a blank line between each line printed on the command line? For example, say I'm grep-ing the log. That way, multiple wrapped lines aren't being confused.

  • Chris Johnsen
    Chris Johnsen over 13 years
    You will probably want to use read -r to avoid treating backslash characters as special.
  • Evan
    Evan over 13 years
    The -e isn't necessary. piping whatever | sed G ought to do the trick.
  • camh
    camh over 13 years
    A slightly simpler sed substitution would be 's/$/\n/', although @Chris Johnsen's 'G' command is even simpler.
  • Dean Hill
    Dean Hill over 13 years
    Why cat and not cp?
  • laurent
    laurent over 13 years
    sure cp would be easier and faster!... lol - I was reading the other answers dealing with grep and awk so I wrote it the cat way but I'm correcting that, thanks
  • Chris Johnsen
    Chris Johnsen over 13 years
    @frabjous: Right, might as well leave off the -e since there we only need a single command argument.
  • Michael Finco
    Michael Finco over 6 years
    Can I give you like, +2? Nails it!
  • biolinh
    biolinh over 6 years
    but, it is only show in stdout, but not save it