简体   繁体   中英

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.

您可以使用除斜杠以外的其他分隔符,例如“ ;

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

Use , instead of / !

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

In fact you can use almost any char as separators.

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

that should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM