1

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.

2
  • grep [...] | cut [...] is often better expressed using awk (and saves you a pipe) Commented Sep 27, 2011 at 9:45
  • In this case, awk '/Built / { print substr($0, 22, 8); exit }' script.sh Commented Sep 27, 2011 at 10:10

3 Answers 3

6

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

sed -i "" "s;$olddate;$newdate;g" script.sh
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks for such a quick response!
3

Use , instead of / !

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

In fact you can use almost any char as separators.

Comments

2

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

that should work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.