Sed Help in Updating something only in one particular file.

Hi,

The scenerio is that I want to replace a text in one particular line of a file. But when I am using the sed it's replacing all the occurences of that text.

Like the file is:

>cat test
DNGGF10 :None :Test
DNGGF11 :ABC :Test1
DNGGF12 :None :Test2
DNGGF13 :None : Test3

I have written the following code for this:-
>cat script
name=$1

sed "s/`grep DNGGF10 test|cut -d ':' -f2`/$1/" test >test.chk

But while executing the code as ./script yogi then I am getting O/p in test.chk as:

>cat test.chk
DNGGF10 :Yogi:Test
DNGGF11 :ABC :Test1
DNGGF12 :yogi:Test2
DNGGF13 :Yogi: Test3

But i want to replce second field only for that line which contains DNGGF1O i.e the 1st line.Also space total sapce in second field should also not get altered.

Kindly shed some light on it.

Thanks-
Yogi

Is this what you're looking for?

sed "/DNGGF10/s/None/$1/" test > test.chk

Regards

Thanks, I got my hint from here. I changed my code to:

sed "/DNGGF10/s/`grep DNGGF10 test|cut -d ':' -f2`/$1/" test >test.chk

Now I am able to replace text only that paritucular line but the space after None are still getting altered which should not happen.

Line before replacing text:

DNGGF10 :None :Test (4 Spaces after None)

After running the script as ./script abc

DNGGF10 :abc:Test

While there should be five spaces after abc.

Kindly suggest.

Regards,
Yogi

If you want fix columns you can use awk:

awk -v var=$1 '
BEGIN{FS=OFS=":"}
/DNGGF10/{$2=sprintf("%-8s", var)}1
' test > test.chk

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards

sed '/DNGGF10/s/\(.*\) \(.*\) \(.*\)/\1 :\1 \3/' test >test.chk