2009-03-25

sed - search & replace with variables within bash

sed is extremely helpful in search & replace text, in file & variables. Often we need to construct a list of strings together with variables. But the normal usage of sed :
e.g.
sed -e 's/text-to-search/string-to-replace-$variable-name/g'

will only replace the variable $variable-name literally rather then the pre-define value. After meddling with a few more stuff, the problem lies on ...

the quotation wrap around the search criteria. It should be double quote rather then single quote example shown above. The below code is in the correct syntax (with corrected quotation marks) :
sed -e "s/text-to-search/string-to-replace-$variable-name/g"

Shalom !!!

No comments: