SUMMARY: Using variables in sed

2007-12-25 5:41:00

Thank you to the many of you that responded so quickly! The answer was
to use double quotes around the sed script rather than single quotes, as
in the following.

Sed -e "s/HOST/$HOST/g" > filename

Very simple but so elusive!

Thanks again!

-----Original Message-----
From: codeprof-bounces at codeprof.com
[mailto:codeprof-bounces at codeprof.com] On Behalf Of Cohen,
Laurence
Sent: Wednesday, January 04, 2006 9:42 AM
To: codeprof at codeprof.com
Subject: Using variables in sed

Hello everyone!

Would anyone have a hint as to how to use a variable from the OS in sed?
For example, I want to be able to substitue the hostname of a system for
every instance of the word HOST in a file. I set the variable
$HOST=`hostname` and then run sed -e 's/HOST/$HOST/g' > filename. The
result of this is that it substitutes the string "$HOST" instead of it's
value. I even tried sed -e 's/HOST/`echo $HOST`/g' > filename, and the
result is that it substitutes the string "`echo $HOST`, which is also
not what I want. How do I get sed to understand that I'm referring to a
variable and not a string?

Thanks!

Laurence H. Cohen
ISM Unix System Administrator
_______________________________________________
codeprof mailing list
codeprof at codeprof.com
http://www.codeprof.com/execute/ask/?codeinfoid=38885

Comments

Got something to say?

You must be logged in to post a comment.