Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed?

The affected portion of my script goes something like this:

A=`cut -d. -f1 $FILE`
B=`cut -d. -f2 $FILE`
C=`cut -d. -f3 $FILE`

sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile

now, I understand that the single quote prevents the shell from interpreting special characters, but if I omit it, the process just hangs there.

Thanks,
kristy

(working with Solaris)

Take the variables out of the single quotes...

sed 's/1111/'${A}'/g;s/2222/'${B}'/g;s/3333/'${C}'/g' file > anotherfile

At least that should always work. For the example you posted, it looks like double quotes would've worked. If the contents of the variables are funky, you may need double quotes around them.

IE change ${A} above to "${A}"

1 Like

Worked great! Thanks for the tip.