Text Processing - How Do I Add Newlines Between Lines Printed On The Command Line - Unix & Linux Stack Exchange
Text Processing - How Do I Add Newlines Between Lines Printed On The Command Line - Unix & Linux Stack Exchange
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
41 aren't being confused.
Share Improve this question Follow edited Jan 24, 2018 at 23:23 asked Oct 14, 2010 at 21:00
Jeff Schaller ♦ xenoterracide
68k 35 119 257 60.2k 77 193 252
sed G
# option: g G Copy/append hold space to pattern space.
67
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.
Share Improve this answer Follow edited Aug 12, 2016 at 12:40 answered Oct 15, 2010 at 2:10
Waldemar Wosiński Chris Johnsen
163 1 5 20.4k 8 66 54
3 The -e isn't necessary. piping whatever | sed G ought to do the trick. – frabjous Oct 15, 2010 at 2:25
https://unix.stackexchange.com/questions/3127/how-do-i-add-newlines-between-lines-printed-on-the-command-line 1/3
11/22/24, 10:43 PM text processing - How do I add newlines between lines printed on the command line? - Unix & Linux Stack Exch…
@frabjous: Right, might as well leave off the -e since there we only need a single command argument.
– Chris Johnsen Oct 15, 2010 at 18:49
Can I give you like, +2? Nails it! – Christian Bongiorno Sep 28, 2017 at 17:40
but, it is only show in stdout, but not save it – biolinh Oct 18, 2017 at 12:20
Use awk to add an extra newline. This also lets you filter out things you don't want.
Share Improve this answer Follow answered Oct 14, 2010 at 21:28
KeithB
3,219 21 13
Use sed and replace the whole line by itself plus one extra newline character:
Share Improve this answer Follow answered Oct 14, 2010 at 21:03
fschmitt
8,860 38 47
4 A slightly simpler sed substitution would be 's/$/\n/', although @Chris Johnsen's 'G' command is even
simpler. – camh Oct 15, 2010 at 5:05
4 sed G
Example:
ls | sed G
https://unix.stackexchange.com/questions/3127/how-do-i-add-newlines-between-lines-printed-on-the-command-line 2/3
11/22/24, 10:43 PM text processing - How do I add newlines between lines printed on the command line? - Unix & Linux Stack Exch…
Share Improve this answer Follow answered Mar 24, 2020 at 19:47
jasonleonhard
593 5 7
grep SPAM mail.log | while read -r line; do echo; echo $line; done
3
Share Improve this answer Follow edited Oct 15, 2010 at 7:51 answered Oct 14, 2010 at 21:04
Nifle
549 2 6 17
You will probably want to use read -r to avoid treating backslash characters as special.
– Chris Johnsen Oct 15, 2010 at 1:57
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
0 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
Share Improve this answer Follow edited Oct 15, 2010 at 13:51 answered Oct 15, 2010 at 1:16
laurent
655 4 7
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, 2010 at 13:49
https://unix.stackexchange.com/questions/3127/how-do-i-add-newlines-between-lines-printed-on-the-command-line 3/3