little sed help

little sed help

am 03.01.2008 18:42:54 von prabato

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.

Re: little sed help

am 03.01.2008 18:57:55 von Stephane CHAZELAS

On Thu, 3 Jan 2008 09:42:54 -0800 (PST), prabato@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

Re: little sed help

am 03.01.2008 19:07:09 von Cyrus Kriticos

prabato@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.

Re: little sed help

am 03.01.2008 19:08:31 von Bill Marcum

On 2008-01-03, prabato@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.

Re: little sed help

am 03.01.2008 19:10:26 von Loki Harfagr

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"

Re: little sed help

am 04.01.2008 03:57:02 von Ed Morton

On 1/3/2008 11:42 AM, prabato@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.

Re: little sed help

am 04.01.2008 07:17:53 von mik3l3374

On Jan 4, 1:42 am, prab...@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