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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference environment variable in sed 2

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
How can I reference an environment variable in sed? e.g. I thought this might work but it doesn't:

DATE=`date +%y%m%d`
echo "Today is " | sed 's/is /is ${DATE}/'

I've tried Google, man sed, O'Reilly, & searching TekTips but haven't been able to find anything.

Thanks, Chris
 
Hi

That is not related to [tt]sed[/tt]. The shell itself is the one who makes the variable expansion. But only in unquoted text and between double quotes ( " ). In texts delimited with single quotes ( ' ) no expansion is performed.
Code:
echo "Today is " | sed [red]"[/red]s/is /is ${DATE}/[red]"[/red]

[gray]# or[/gray]

echo "Today is " | sed 's/is /is [red]'"[/red]${DATE}[red]"'[/red]/'
By the way, if you want to append a string with [tt]sed[/tt] :
Code:
echo "Today is " | sed 's/$/'"${DATE}"'/'
I assume your reason for involving [tt]sed[/tt] is learning, otherwise you would simply do :
Code:
echo "Today is `date +%y%m%d`"
Or
Code:
date '+Today is %y%m%d'

Feherke.
 
Thanks Feherke

Yes - I was trying to learn - what I'm trying to do is more messy, using DATE seemed a clearer way to desribe my problem - which I no longer have!

Chris

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top