Using sed inside shellscript

Hi All,

I am trying to use sed command inside a shell script.
The same sed command is working on command line, however its not working while using inside a shell script.
From various sources i found , it could be done by using -i option, but its not working as well.

sed 's/something/otherone/g' inputfile

its working perfectly filne while using on command line.

#! /bin/ksh
sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' full/path/inputfile

Tried using absolute path of sed, and inputfile but still its not working.
ANy pointer on this please.

NOTE: I am using SunOS

Thanks
Goutam

Hello gotamp,

If I am seeing it correctly you have space in between #! and /bin/ksh in your shebang, try without the space as #!/bin/ksh and let me know how it goes then.

Thanks,
R. Singh

1 Like

Thanks R.Singh for correctly pointing it out.
I have done the changes accordingly, but still it does not work.
Not sure, if sed could be used this way inside a shell script.

THanks
Goutam

Any error messages? Run with -x option set and post the result...

Hi RudiC

Please find my shell script that uses sed

#!/bin/ksh
a=$1
b=$2
echo "a is $a , b is $b"
/usr/bin/sed 's/$a/$b/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

and this is the data contained in the file filetosedreplace

D1: cat /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

and now, this is the output when i run the script in debug mode

D1: sh -x sedinshell.ksh one ONE
a=one
b=ONE
+ echo a is one , b is ONE
a is one , b is ONE
+ /usr/bin/sed s/$a/$b/g /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

additional info

D1: uname -a
SunOS simsun2 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise

Hello gotamp,

You are passing variables to the script, so could you please try following(haven't tested though).

/usr/bin/sed 's/'${a}'/'${b}'/g' /user/iomaint/tempo/gtm/sed/filetosedreplace
OR
/usr/bin/sed 's/"{$a}"/"{$b}"/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

Thanks,
R. Singh

1 Like

Your samples in post#5 show a TOTALLY different picture that you have in post#1!

Within single quotes, the shell does NOT expand variables, so sed 's/$a/$b/g' tries to replace the string constant "$a" (which, of course, is not found in your file) with the string constant "$b" - as you can see in your xtrace output as well. Try double quotes instead, or the proposal by RavinderSingh13.

1 Like

And, by the way, it wouldn't have worked when entered interactively ("on command line"), either.

Many Thanks Ravinder for the solution
And Thanks Rudic For the explanation.

Ravinder,
The single quote one worked.

/usr/bin/sed 's/'${a}'/'${b}'/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

and the double quote one is not working as one can expect/ assume.

Thanks
Goutam

---------- Post updated at 08:50 AM ---------- Previous update was at 08:45 AM ----------

Hi RudiC,

I think I am not that good at explaining things/ issues.
Yes in command line , i was just using values in sed command and not using variables.

Will try to be more precise while narrating issues henceforth.
Anyways thanks again for the help. Happy that issue got resolved and most importantly i realized the fault.

How about

sed "s/$a/$b/g" /user/iomaint/tempo/gtm/sed/filetosedreplace

given neither $a nor $b contain characters with special meaning when used inside regexes.

In more complex expressions, where everything in quotes is too cumbersome, the shell variable references should be in quotes nevertheless (where the quotes must appear in the shell context, so the shell substitutes them and they do not appear in the sed context)

sed 's/'"${a}"'/'"${b}"'/g' /user/iomaint/tempo/gtm/sed/filetosedreplace
1 Like

Yes This too works.
Thanks RudiC. Brilliant !!