Reputation: 16436
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?
Upvotes: 4
Views: 7328
Reputation: 499
try this................... sed -e 's/%d/'$a'/g' amar.htm , amar.htm having the string "%d" which is indeed to be replaced and "a" is having the string to replace.
Upvotes: 0
Reputation: 204678
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.
Upvotes: 1
Reputation: 182772
Use double quotes instead of single quotes.
Because '
quoting prevents shell variable expansions, and "
quoting does not.
Upvotes: 17