Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed Variable Substitution

Status
Not open for further replies.

gregsimpson

Programmer
Apr 18, 2002
521
Hi,

I have a script using sed, which is getting ite commands for sed from a file, as I wish to use multiple commands. Unfortunately using the file in this way to drive sed, doesn't appear to do substitution of variables.

If this is something I'm stuck with can someone else recommend a neat way to process my file please. What I'm trying to do is change a number of parameter markers in the data file, with variables I have exported and are referenced in the file I use to drive sed.

Thanks
Greg
 
Any chance you could post your code and your sed file ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

I'm sorted thanks. Built the sedfile dynamically so it contained my parameter markers expanded.

Thanks for taking the time to reply, much appreciated.

Greg
 
Greg, don't just bail on us, Tek-Tips is about sharing solutions.

Without seeing any code, I'd like to guess! Are you using single quotes in your [tt]sed[/tt] command? You should use double quotes if you need to do variable translation in the command.

For example, this WON'T work...
Code:
cat sed_commands.txt | while read FROM TO FILE
do
    mv ${FILE} ${FILE}.orig
    sed [b][red]'[/red][/b]s/${FROM}/${TO}/g[b][red]'[/red][/b] ${FILE}.orig > ${FILE}
done
...but this WILL WORK...
Code:
cat sed_commands.txt | while read FROM TO FILE
do
    mv ${FILE} ${FILE}.orig
    sed [b][red]"[/red][/b]s/${FROM}/${TO}/g[b][red]"[/red][/b] ${FILE}.orig > ${FILE}
done
Is that it?
 
Sambones,

Thanks for the answer!

At least you fixed <My> problem!!!

Now if I could only figure out why is worked.... =)
 
The single quotes (') don't allow anything within them to be altered by the shell before the line is executed. The double quotes (") allow the shell to parse the contents before executing the line. This means that the double quotes (") allows variable substitutions with them.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top