0

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?

jruivo
  • 447
  • 1
  • 8
  • 22
  • Possible duplicate of [sed substitution with bash variables](https://stackoverflow.com/questions/7680504/sed-substitution-with-bash-variables) – Sundeep Jul 01 '19 at 10:10
  • 1
    Possible duplicate of [Replace a string in shell script using a variable](https://stackoverflow.com/questions/3306007/replace-a-string-in-shell-script-using-a-variable) – Wiktor Stribiżew Jul 01 '19 at 10:11

1 Answers1

2

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Prakash Krishna
  • 1,239
  • 6
  • 12