Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to create a bash script where I can replace a date in a filename with the current date, however, I'm not being able to do so.

Here's what I have so far:

#!/bin/bash

my_file="FILENAME_20170410235908_GTT_DEV_20170410235400_20170410235408_XX_YY.nc"

my_date=`date "+%Y%m%d"`

echo "$my_file" | sed  's/([0-9]{12})/"${my_date}"/g'

I'm currently getting this:

FILENAME_"${my_date}"08_GTT_DEV_"${my_date}"00_"${my_date}"08_XX_YY.nc

Howerver, this is what I'd like to have:

FILENAME_2019070135908_GTT_DEV_20190701235400_20190701235408_XX_YY.nc

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
300 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can try

sed "s/([0-9]+)/${my_date}/g"

single quote will not replace variable data. my_date will have only date. If you want the timestamp also, add it from the date command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...