我需要以YYYY-MM-DD格式获取上个月的日期,假设如果在任何日期在5月运行脚本,那么它应该以格式2018-04-30返回我4月的最后日期。Would be good if can get the same work done with awk statement also.我尝试了下面的代码,并能够以下面的形式获取输出,但我需要的格式为YYYY-MM-DDApr 30 2018代码:#!/bin/bashcur_month=`date +%m`cur_year=`date +%Y`prev_month=$(($cur_month-1))# Check to see if this is Januaryif [ $prev_month -lt 1 ]thenprev_year=$(($cur_year-1))prev_month=12LastDayOfpreviousMonth=`cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$NF,$2}'`elseLastDayOfpreviousMonth=`cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$NF,$2}'`fiecho $LastDayOfpreviousMonth输入文件:在该文件的第14列中,需要以YYYY-MM-DD(2018-04-30)的形式放入上个月的日期值EPOS|EPOS_OUTWARD_B2B_OSR_TTML_TTSL_$LDPM_20180504_V1.0.txt|||AD|Outward|ANP|Receipt cum invoice|42018|37AAACT2438A1ZS|INV|TAX|IN37201800001452|2018-04-17||||1||||SOHANRAJ RAJ KUMAR||||37||||||9984||Telecommunication services||||300|0||9|27|9|27|||||354||||||||||EPOS|EPOS_OUTWARD_B2B_OSR_TTML_TTSL_$LDPM_20180504_V1.0.txt|||AD|Outward|ANP|Receipt cum invoice|42018|37AAACT2438A1ZS|INV|TAX|IN37201800001426|2018-04-12||||1||||MOLLITE SIVARAMA KRISHNA||||37||||||9984||Telecommunication services||||300|0||9|27|9|27|||||354||||||||||EPOS|EPOS_OUTWARD_B2B_OSR_TTML_TTSL_$LDPM_20180504_V1.0.txt|||GJ|Outward|GUJ|Receipt cum invoice|42018|24AAACT2438A1ZZ|INV|TAX|IN24201800000651|2018-04-07||||1||||AIREN LTD ||||24||||||9984||Telecommunication services||||500|0||9|45|9|45|||||590||||||||||
2 回答
DIEA
TA贡献1820条经验 获得超2个赞
GNU date本身能够完美而可靠地处理此任务,而无需其他魔术。
$ date -d "$(date +%Y-%m-01) - 1 day" "+%Y-%m-%d"
2018-04-30
- 2 回答
- 0 关注
- 279 浏览
添加回答
举报
0/150
提交
取消