3

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?

2

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.