Computing.Net > Forums > Unix > pattern matching/replacing with sed

pattern matching/replacing with sed

Reply to Message Icon

Original Message
Name: MVal
Date: January 11, 2005 at 13:41:09 Pacific
Subject: pattern matching/replacing with sed
OS: HPUX
CPU/Ram: HP
Comment:

Hi,

I am trying to do the following simple operation with sed, but it wouldnt work because sed doesn't allow me to use variables in the replace string. Anyway to do it ?

testvar="TestFilename.#.txt"
testdate=`date +%m%d%Y`

resultvar=`echo $testvar | sed -e s/#/$testdate`

Here $testdate in the expression fails.

Basically I want to replace the "#" with today's date.

Your help is appreciated ...

regards
-MVal


Report Offensive Message For Removal


Response Number 1
Name: vgersh99
Date: January 11, 2005 at 13:53:38 Pacific
Reply: (edit)

resultvar=`echo $testvar | sed -e "s/#/$testdate/"`


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: January 11, 2005 at 13:55:17 Pacific
Reply: (edit)

You forgot the terminate /:

resultvar=`echo $testvar | sed -e s/#/$testdate/`


Report Offensive Follow Up For Removal

Response Number 3
Name: MVal
Date: January 11, 2005 at 14:02:28 Pacific
Reply: (edit)

Thanks vgersh99, putting the replace statement in double quote solves the problem..

Thank nails, yes I forgot the ending '/' also...



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: pattern matching/replacing with sed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge