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!

How to do value-substitution in sed/awk?? 1

Status
Not open for further replies.

micotao

Programmer
Jun 16, 2004
33
CN
Hi, All:
Although we can do pattern match and corresponding substitution in Awk and Sed, I find it very disable when we want to do value-substitution.I mean:say, when I want to replace the words which equal to the value of $var1(say,$var1="charlie", we want to replace where it is charlie to daniel,here $var2=daniel) in file "file_test1",and we can't know what string to replace because var1,var2 is dynamically assigned value.How to do such "value-substitution"??? e.g.:
sed 's/$var1/$var2/g' file_test1
this will fully disobey what we want it to do!!!it will do pure pattern substitution,$var1--->$var2, but we want it to replace all "charlie" ---->"daniel".
PLEASE TELL ME HOW TO SOLVE SUCH A PROBLEM???We have to do such work in our work, THIS IS IN VERY URGENT NEED!!!!
help,please.
 
Have you tried this ?
sed "s/$var1/$var2/g" file_test1
In your shell's man pages take a look at quoting
And please take a look at either one faq in my sig.
Howl that your needs is urgent is not a professional manner to use this forum, particularly when the reply is in your online documentation.
BTW, for awk: man awk, the -v option.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It WORKS,forgive my ignorance, I will look into man pages.
Thanks a lot for PHV.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top