On 16-Aug-07 08:15:04, Alexey Eremenko wrote:
Hi all !
I would like to insert a new line on each second line, like that:
pre.txt: line1 line2 line3
After.txt: line1
line2
line3
I have found the "echo -e "\n" " command to actually insert a new line, but how to automate the process so every second line will get a new line?
Your pre.txr and aAfter.txt example suggests that you are not inserting
a new line at each second line of the original, which would give
pre.txt
line1
line2
line3
line4
line5
...
After.txt
line1
line2
line3
line4
line5
...
but instead inserting an extra newline after each line of pre.txt
Either way, though, you could use 'awk':
cat pre.txt | awk '{print $0 "\n"}'
will do the insert of the extra newline after each line of pre.txt;
cat pre.txt | awk 'BEGIN{R=0}{if(R==0) {print $0} else
{print $0 "\n"}}{R = 1-R}'
will do it after each second line of the original. Example:
$ cat pre.txt
line1
line2
line3
line4
line5
line6
line7
line8
]$ cat pre.txt | awk '{print $0 "\n"}'
line1
line2
line3
line4
line5
line6
line7
line8
$ cat pre.txt | awk 'BEGIN{R=0}{if(R==0) {print $0} else
{print $0 "\n"}}{R = 1-R}'
line1
line2
line3
line4
line5
line6
line7
line8
This is based on testing the value of R which switches
between 0 and 1. Clearly it is straightforward to compute
more complicated conditions involving the line-number of
the original file.
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding)