Reputation: 111
I've a huge amount of file that are named like A2012178.1220.051.2012178233531.hdf There, from the 2nd character to the 8th is the date, year plus julian day. From the 13rd character to the extention of the file the name is rubbish to me... so I would like to rename the files and convert the date (for convenience).
So far I'm able to convert the date in bash
CONVERTED=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
But I have no idea of how to read the year and julian day from the file name and replace the name within the bash script....
Any idea?
Upvotes: 1
Views: 108
Reputation: 54541
The tricky part is that the julian day has a variable length, so you don't know ahead of time how to slice it out. So, you might do something like this:
read begin date end < <(echo $filename | sed -e 's/\(.\)\([[:digit:]]\+\)\(.*\)\
/\1 \2 \3/')
Year=${date::4}
JulianDay=${date:4}
newdate=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
mv $filename "${begin}${newdate}${end}"
This basically splits up the file name pulling out the date field and then grabbing the first 4 digits as the year with the rest being the julian day.
(This assumes the julian day isn't 0 padded, which isn't clear from your question. If it's zero padded, it's much easier).
Upvotes: 0
Reputation: 17188
Use parameter substitution to get the substrings:
name="A2012178.1220.051.2012178233531.hdf"
Year=${name:1:4}
JulianDay=${name:5:3}
Upvotes: 2