[opensuse] How to insert lines into text file ?
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 ? please help -- -Alexey Eremenko "Technologov" -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Thu, 2007-08-16 at 10:15 +0200, 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 ?
please help
Quick and easy; for i in `cat pre.txt`; do echo -e "$i\n"; done >after.txt If that's all you want to do... If you do other sort of text manipulation, have a look at sed. Cheers, Magnus -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hi, On 8/16/07, Magnus Boman <captain.magnus@gmail.com> wrote:
On Thu, 2007-08-16 at 10:15 +0200, 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 ?
please help
Quick and easy; for i in `cat pre.txt`; do echo -e "$i\n"; done >after.txt
If that's all you want to do... If you do other sort of text manipulation, have a look at sed.
Or just use sed: cat your_file | sed G > new_file -- Mark Goldstein -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Aug 16 2007 11:26, Mark Goldstein wrote:
I would like to insert a new line on each second line, like that:
Quick and easy; for i in `cat pre.txt`; do echo -e "$i\n"; done >after.txt
That will not work, and for good. You should never-never-never use "for i in `something`" for anything unless you know exactly what the outcome is. Because by default, unless you muck with $IFS, it splits at word boundaries, not lines.
If that's all you want to do... If you do other sort of text manipulation, have a look at sed.
Or just use sed:
cat your_file | sed G > new_file
perl -i -pe 's/\n/\n\n/' new_file Jan -- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Thursday 16 August 2007 01:52, Jan Engelhardt wrote:
On Aug 16 2007 11:26, Mark Goldstein wrote:
I would like to insert a new line on each second line, like that:
Quick and easy; for i in `cat pre.txt`; do echo -e "$i\n"; done >after.txt
That will not work, and for good. You should never-never-never use "for i in `something`" for anything unless you know exactly what the outcome is. Because by default, unless you muck with $IFS, it splits at word boundaries, not lines.
While you're right that as written this probably won't work and it's really a distinctly sub-optimal solution, it will work if you set the IFS variable to be newline only (by default, it's space, tab and newline). Then the "words" into which the input is parsed will be individual lines. Others have posted better solutions.
If that's all you want to do... If you do other sort of text manipulation, have a look at sed.
Or just use sed:
cat your_file | sed G > new_file
Most commands, sed included, read named files and don't require the use of a separate command and a pipe. This works as well and has slightly less overhead: sed your_file -e G >new_file
perl -i -pe 's/\n/\n\n/' new_file
Jan
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
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) <efh@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 16-Aug-07 Time: 09:53:12 ------------------------------ XFMail ------------------------------ -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (6)
-
Alexey Eremenko
-
efh@nessie.mcc.ac.uk
-
Jan Engelhardt
-
Magnus Boman
-
Mark Goldstein
-
Randall R Schulz