views:

641

answers:

2

Alright, I know this is a simple question, but I can't seem to get this sed command to work. I'm trying to get a text file and replace one bit of it from placeholder text to a study code. The study code that it is going to replace it with is passed into the script using arguments when the script is first ran. The problem is, when I try to replace the placeholder text with the variable $study, it replaces it with a literally "$study".

Right now my arguments set like this:

export study=$1

export tag=$2

export mode=$3

export select=$4

My sed command looks like this:

sed -i.backup -e 's/thisisthestudycodereplacethiswiththestudycode/$study/' freq.spx

Is there some easy way of getting sed to not look at the literal $study, or would it be better at this point to do it another way?

+8  A: 

Use double quotes instead of single quotes.

Because ' quoting prevents shell variable expansions, and " quoting does not.

Paul Tomblin
Well, that did it. Thanks for the help!
Dropped.on.Japan
Thanks for expanding the answer, @dmckee.
Paul Tomblin
A: 

You probably won't run into this issue, but just in case...

Paul's answer is slightly suboptimal if $study might contain slashes or other characters with special meaning to sed.

mv freq.spx freq.spx.backup && \
    awk -v "study=$study" '{
        gsub(/thisisthestudycodereplacethiswiththestudycode/, study);
        print;
    }' freq.spx.backup > freq.spx

Although awkward (hah, pun!), this will always work regardless of $study's contents.

ephemient