Sed - variable substitution

Problem with the code below is that the value of the variable is not getting substituted in the sed expression.

#/bin/csh
set UNIX_ID="rajibd"
set X_ID="xrajibd"
sed -n 's/$UNIX_ID/$X_ID/g' passwd

When run , it is not giving expected output as shown below :

adroit:/home/seo/hitendra 71 ] sh -x change
+ set UNIX_ID=rajibd
+ set X_ID=xrajibd
+ sed -n s///g passwd

(1) You are writing a C-Shell script and (2) You are invoking it with sh - a bourne-derived shell - to run with the -x option. (3) The sed invocation may be wrong as you will not see any output, changed or otherwise. Did you really mean the g for global substitute, or p for print the result of change?

Also, that first line is wrong - it should be

#!/bin/csh

Andrew

I rectified the shabang line.
But the change is not reflected in the passwd file.

It won't - sed does not work like that. If you are using a Linux system you can use the -i or --in-place option, ie

sed -i.old "s/$UNIX_ID/$X_ID/g" passwd

If you are not using Linux, and the sed you have is not a gnu sed try this instead:

cp -p passwd passwd-old
sed "s/$UNIX_ID/$X_ID/g" passwd > passwd-new
cp passwd-new passwd
rm passwd-new

While you could just rename passwd into passwd-old and then redirect the output of sed directly into a new passwd file this should help you back out if there are problems with the script.

Andrew