From: onlineviewer on
Hi Guys,

I need to add a line to a file, 2nd from the top. What would be the
easist way.

Thanks,

From: Icarus Sparry on
On Wed, 22 Mar 2006 10:37:13 -0800, onlineviewer wrote:

> Hi Guys,
>
> I need to add a line to a file, 2nd from the top. What would be the
> easist way.
>
> Thanks,

Is this homework?

2i\
This is the new second line

The only complicated bit is how to get the quoting right. For an 'sh' type
shell, just put the command in single quotes.

sed -e '2i\
new second line' filename

For a 'csh' you might be lucky with

sed -e '2i\\
new second line' filename


From: Xicheng on
Icarus Sparry wrote:
> On Wed, 22 Mar 2006 10:37:13 -0800, onlineviewer wrote:
>
> > Hi Guys,
> >
> > I need to add a line to a file, 2nd from the top. What would be the
> > easist way.
> >
> > Thanks,
>
> Is this homework?
>
> 2i\
> This is the new second line
>
> The only complicated bit is how to get the quoting right. For an 'sh' type
> shell, just put the command in single quotes.
>
> sed -e '2i\
> new second line' filename
>
> For a 'csh' you might be lucky with
>
> sed -e '2i\\
> new second line' filename

sed '1s/$/\nnew second line/' myfile.txt

Xicheng

From: Geoff Clare on
"Xicheng" <xicheng(a)gmail.com> wrote, on Wed, 22 Mar 2006:

> sed '1s/$/\nnew second line/' myfile.txt

That probably only works with GNU sed. Most versions of sed
don't treat \n in the replacement string as special.

--
Geoff Clare <netnews(a)gclare.org.uk>

 | 
Pages: 1
Prev: awk command
Next: inserting text into files