little sed help

I need sed to delete line 130 and then replace it with the $shell_var

sed -e '130s/ d'
^^^ doesn't work

sed -e '130i/$shell_var/'
^^^ doesn't work

please help.
prabato [ Do, 03 Januar 2008 18:42 ] [ ID #1898559 ]

Re: little sed help

On Thu, 3 Jan 2008 09:42:54 -0800 (PST), prabato [at] gmail.com wrote:
> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work


sed "130s/.*/$shell_var/"

assuming $shell_var doesn't contain any /, \, & or newline
character.

awk -v repl="$shell_var" 'NR == 130 {$0 = repl} {print}'

assuming $shell_var doesn contain \ characters.

Or

sed "130!b
i\\
$shell_var
d"

assuming $shell_var doesn't contain \ or newline characters.

--
Stephane
Stephane CHAZELAS [ Do, 03 Januar 2008 18:57 ] [ ID #1898560 ]

Re: little sed help

prabato [at] gmail.com wrote:
> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work

sed "130{i $shell_var
d}" filename

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Do, 03 Januar 2008 19:07 ] [ ID #1898561 ]

Re: little sed help

On Thu, 03 Jan 2008 09:42:54 -0800, prabato wrote:

> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work
>
> please help.

This would do, provided the content of $shellVar
won't hide bovver chars:

sed -n "/130/{g;s/^$/$shellVar/};p"
Loki Harfagr [ Do, 03 Januar 2008 19:10 ] [ ID #1898562 ]

Re: little sed help

On 2008-01-03, prabato [at] gmail.com <prabato [at] gmail.com> wrote:
>
>
> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
What are you trying to do here? The s command requires three
separators:
s/pattern/replacement/

>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work
>
Shell variables aren't substituted inside single quotes. Use double
quotes, if that's what you want.

> please help.
Bill Marcum [ Do, 03 Januar 2008 19:08 ] [ ID #1898564 ]

Re: little sed help

On 1/3/2008 11:42 AM, prabato [at] gmail.com wrote:
> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work
>
> please help.

Assuming you don't really NEED to use sed:

awk -v var="$shell_var" '{print NR==130?var:$0}' file

Ed.
Ed Morton [ Fr, 04 Januar 2008 03:57 ] [ ID #1899382 ]

Re: little sed help

On Jan 4, 1:42 am, prab... [at] gmail.com wrote:
> I need sed to delete line 130 and then replace it with the $shell_var
>
> sed -e '130s/ d'
> ^^^ doesn't work
>
> sed -e '130i/$shell_var/'
> ^^^ doesn't work
>
> please help.


use this, for very large files

#!/bin/sh
head -129 file > outfile
echo "my string" >> outfile
tail +131 file >> outfile
#or more +131 file >> outfile
mik3l3374 [ Fr, 04 Januar 2008 07:17 ] [ ID #1899384 ]
Linux » comp.unix.shell » little sed help

Vorheriges Thema: Regular expression with egrep question...
Nächstes Thema: loading shared libraries: libstdc++.so.6