40

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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
xenoterracide
  • 57,918
  • 74
  • 184
  • 250

6 Answers6

66
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.

Chris Johnsen
  • 19,790
  • 8
  • 63
  • 52
10

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

awk '{print $0,"\n"}' | less
KeithB
  • 3,159
  • 20
  • 13
7

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

grep foo /var/log/maillog | sed -e "s/^.*$/&1\n/"
fschmitt
  • 8,720
  • 35
  • 46
  • 4
    A slightly simpler sed substitution would be 's/$/\n/', although @Chris Johnsen's 'G' command is even simpler. – camh Oct 15 '10 at 05:05
3

Is this what you are after?

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

Nifle
  • 539
  • 2
  • 6
  • 17
3

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.

jasonleonhard
  • 523
  • 5
  • 7
0

If it's for more than just have look, I prefer to send them to a text file and open with a text editor so you can set the lines to wrap or not and do searches easily... and delete the unwanted lines and so on without having to type a lot of commands.

cat file.log > log.txt and gedit log.txt or a terminal editor like nano

Edit: or cp file.log log.txt wich is of course easier and faster... thanks to KeithB comment

laurent
  • 655
  • 4
  • 7
  • 1
    Why `cat` and not `cp`? – KeithB Oct 15 '10 at 13:21
  • 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 – laurent Oct 15 '10 at 13:49