0

I have a set of file names following some pattern. I need to insert a date into the filename string. Now, I also have some files following a slightly different pattern. I would like to know how can a single string match both the requirements.

eg. filenames are like:

abcTrade_proj.dat 
abcPosition_proj.dat 
abcAccount_proj.dat

I'm using below string to construnct the filename like abcTrade_yyyymmdd_proj.dat

${filename%_*}_${DATE}_${filename#*_}

Now, apart from the above file patterns, I have the pattern:

abc_pqr_Account_proj.dat 
abc_pqr_Trade_proj.dat

All i need to do is cut the string till the last _, insert the date and append the string that is there after _, for both the type of file patterns.

Is there any solution for both situations?

2
  • what you mean by "till last ''" ? Commented Jun 14, 2014 at 9:17
  • it is like, i need to split the string based upon last _. so that irrespective of no.of unserscores in the first part of the filename, i can always split based upon the last uderscore. Commented Jun 14, 2014 at 10:05

1 Answer 1

1

If possible, you could use sed to construct the new name as follows:

DATE=$(date +%Y%m%d)
echo "abcTrade_pqr_proj.dat" | sed "s/^\(.*\)_\([^_]*\)/\1_${DATE}_\2/g" 

which results in:

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

2 Comments

May I suggest export DATE=$(date +%Y%m%d) for the date of current day, at same format?
THanks Eric. sed can be used to cut the strings. I shall test and let you know.

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.