Question

I get a source file in the format of SOURCE_FILE_042014.CSV which contains data for the month of April in 2014. . From this I need to get the last saturday of that month and load that data against this date in our system. For example - the date for which the data of the file SOURCE_FILE_042014.CSV should be 04262014. After I do a substring using cut command and with help from here, I get only the date as 26. How to display the complete week date?

Was it helpful?

Solution

You need to extract the year and month from the filename to be able to ask for the last Saturday. So, just get the day back and compose it back with the year and month you already extracted:

#! /bin/bash
filename=SOURCE_FILE_042014.CSV
date=${filename##*_}
date=${date%.CSV}

month=${date:0:2}
year=${date:2}

day=$(cal $month $year | awk 'NF>6{a=$7} END{print a}')

echo $month$day$year
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top