Will
Will

Reputation: 2410

How to evaluate date from a string

Here are my files :

conf.txt

case1:fooYYYYmmdd.bar

case2:helloYYYYmmdd.world

script.sh

fname=`grep $1 conf.txt | cut -d ':' -f2`

When calling my script.sh with a parameter being case1 or case2, I obviously get my var fname fed with fooYYYYmmdd.bar or helloYYYmmdd.world.

How could I evaluate the date from my conf.txt so fname would be set with foo20181120.bar and hello20181120.world without going through breaking and building back my string ?

Upvotes: 1

Views: 154

Answers (1)

ashish_k
ashish_k

Reputation: 1571

You could just declare a Date variable and use it in fname, like this:

Date=$(date +%Y%m%d)

fname=$(grep $1 conf.txt |sed "s/YYYYmmdd/$Date/g"|cut -d ':' -f2)
echo $fname

Upvotes: 1

Related Questions