我需要以YYYY-MM-DD格式获取上个月的日期,假设如果在任何日期在5月运行脚本,那么它应该以格式2018-04-30返回我4月的最后日期。
Would be good if can get the same work done with awk statement also.
我尝试了下面的代码,并能够以下面的形式获取输出,但我需要的格式为YYYY-MM-DD
Apr 30 2018
代码:
#!/bin/bash
cur_month=`date +%m`
cur_year=`date +%Y`
prev_month=$(($cur_month-1))
# Check to see if this is January
if [ $prev_month -lt 1 ]
then
prev_year=$(($cur_year-1))
prev_month=12
LastDayOfpreviousMonth=`cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$NF,$2}'`
else
LastDayOfpreviousMonth=`cal $prev_month $cur_year | grep -v "^$" | sed -n '1p;$p' | tr "\n" " " | awk '{print substr($1,1,3),$NF,$2}'`
fi
echo $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||||||||||
DIEA