获取特定月份和年份的最后一个星期六日期

时间:2022-08-25 10:30:59

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?

我收到了SOURCE_FILE_042014.CSV格式的源文件,其中包含2014年4月份的数据。由此我需要获得该月的最后一个星期六并在我们的系统中加载该日期的数据。例如 - 文件SOURCE_FILE_042014.CSV的数据应为04262014的日期。在使用cut命令执行子字符串并在此处获得帮助后,我只将日期设置为26.如何显示完整的工作日期?

1 个解决方案

#1


1  

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

#1


1  

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