Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help replacing a work in a file with a variable plus a word.

0 views
Skip to first unread message

cden

unread,
Dec 29, 2009, 8:37:08 PM12/29/09
to
I am a new to scripting. I am working with a script to replace the
word 'name' in a file with the name of the server. I am trying pass a
variable in a search and replace command using sed. It works now, but
only replaces the word 'name' with the '${SERVER}: name' and not the
actual hostname.

TMPDIR="/rae/tmp"
SERVER="`hostname`"

sed -i 's/name:/${SERVER}: name/g' $TMPDIR/mounts.$SERVER


Currently in file
------------------------

name: chris

Trying to make the output to the file mounts.$SERVER become:
-------------------------------------------------------------------------------------------

cltgap01: name: chris

Could awk or gawk work better for this? Please help.

Thanks,
Chris

Chris F.A. Johnson

unread,
Dec 29, 2009, 9:40:55 PM12/29/09
to

Variables are not expanded inside single quotes.

TMPDIR=/rae/tmp
SERVER=`hostname`

sed -i.bak 's/name:/${SERVER}: name:/' "$TMPDIR/mounts.$SERVER"


--
Chris F.A. Johnson, author | <http://cfajohnson.com>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence

Chris F.A. Johnson

unread,
Dec 30, 2009, 9:04:30 AM12/30/09
to
On 2009-12-30, Chris F.A. Johnson wrote:
> On 2009-12-30, cden wrote:
>> I am a new to scripting. I am working with a script to replace the
>> word 'name' in a file with the name of the server. I am trying pass a
>> variable in a search and replace command using sed. It works now, but
>> only replaces the word 'name' with the '${SERVER}: name' and not the
>> actual hostname.
>>
>> TMPDIR="/rae/tmp"
>> SERVER="`hostname`"
>>
>> sed -i 's/name:/${SERVER}: name/g' $TMPDIR/mounts.$SERVER
>>
>>
>> Currently in file
>> ------------------------
>>
>> name: chris
>>
>>
>>
>> Trying to make the output to the file mounts.$SERVER become:
>> -------------------------------------------------------------------------------------------
>>
>> cltgap01: name: chris
>
> Variables are not expanded inside single quotes.
>
> TMPDIR=/rae/tmp
> SERVER=`hostname`
>
> sed -i.bak 's/name:/${SERVER}: name:/' "$TMPDIR/mounts.$SERVER"

Which should, of course, have read:

sed -i.bak "s/name:/${SERVER}: name:/" "$TMPDIR/mounts.$SERVER"

Followups redirected to comp.unix.shell where this belongs.

0 new messages