Unix Shell - little sed help

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2008 > little sed help





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author little sed help
prabato@gmail.com

2008-01-03, 1:23 pm

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.
Stephane Chazelas

2008-01-03, 1:23 pm

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
Cyrus Kriticos

2008-01-03, 1:23 pm

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.
loki harfagr

2008-01-03, 1:23 pm

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"
Bill Marcum

2008-01-03, 1:23 pm

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

Ed Morton

2008-01-04, 1:38 am



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.

mik3l3374@gmail.com

2008-01-04, 1:38 am

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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com