Mailinglist Archive: opensuse (2086 mails)
| < Previous | Next > |
Re: [opensuse] How to insert lines into text file ?
- From: Randall R Schulz <rschulz@xxxxxxxxx>
- Date: Thu, 16 Aug 2007 07:15:50 -0700
- Message-id: <200708160715.50870.rschulz@xxxxxxxxx>
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
> 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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |