0

I have a variable value "Cash_20200413_US_02.dat" and I want to rename it as "Cash_20200413_US_*.dat".

So basically I want to find : a) the last occurence of string "_" and b) string "." and then replace the value between these two strings to "*"

Input="Cash_20200413_US_02.dat"

Output="Cash_20200413_US_*.dat"

0

Given

Input="Cash_20200413_US_02.dat"

you can get the substring up to the last underscore (excluded) with:

echo "${Input%_*}"

and the substring from the last dot (excluded) with:

echo "${Input/*.}"

Combining both, with the requested string in between:

Output="${Input%_*}_*.${Input/*.}"
1
  • Thanks a lot ! Your command gave me the desired output. – Praseetha Nair Jan 12 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.