Josh Fradley
Josh Fradley

Reputation: 562

Replace text using sed

i am having trouble replacing the modified date in my script via sed.

I am getting the last modified date like this:

olddate=`grep -m1 "Built " script.sh | cut -c 22-29`

I get the current date with:

newdate=`date +%d/%m/%y`

Basically i want to replace old date with new date

sed -i "" "s/$olddate/$newdate/g" script.sh

But this doesn't work as the date contains slashes. I've looked around and i can't find the way to escape them properly. Any help would be appreciated.

Upvotes: 1

Views: 2730

Answers (3)

Matthew Farwell
Matthew Farwell

Reputation: 61705

You can use separators other than slashes, for instance ";"

sed -i "" "s;$olddate;$newdate;g" script.sh

Upvotes: 6

Kent
Kent

Reputation: 195049

use sed "s#$olddate#$newdate#g"

that should work

Upvotes: 2

VGE
VGE

Reputation: 4191

Use , instead of / !

sed -i "" "s,$olddate,$newdate,g" script.sh

In fact you can use almost any char as separators.

Upvotes: 3

Related Questions