Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using date variable inside sed command

Tags:

shell

unix

sed

I am storing date inside a variable and using that in the sed as below.

DateTime=`date "+%m/%d/%Y"`

Plc_hldr1=`head -$i place_holder.txt | tail -1 | awk -F ' ' '{ print $1 }'`
Plc_hldr2=`head -$i place_holder.txt | tail -1 | awk -F ' ' '{ print $2 }'`

sed "s/$Plc_hldr1/$DateTime/;s/$Plc_hldr2/$Total/" html_format.htm >> /u/raskar/test/html_final.htm

While running the sed command I am getting the below error.

sed: 0602-404 Function s/%%DDMS1RT%%/01/02/2014/;s/%%DDMS1C%%/1235/ cannot be parsed.

I suppose this is happening as the date contains the following output which includes slashes '/'

01/02/2014

I tried with different quotes around the date. How do I make it run?

like image 931
user3055262 Avatar asked Oct 03 '22 00:10

user3055262


1 Answers

Change the separator to something else that won't appear in your patterns, for example:

sed "s?$Plc_hldr1?$DateTime?;s?$Plc_hldr2?$Total?"
like image 162
janos Avatar answered Oct 13 '22 12:10

janos