我希望编写一个Shell脚本,该脚本在每次运行工作流时查找并替换参数值。我是一个初学者,正在尝试进一步了解这一点。
例如
-$$mp_Custom_Filter_D_Prem=AND BLSB.BLSB_CREATE_DTM <= '2019-02-28'
--$$mp_Custom_Filter_D_LEP=AND convert(date,(substring(SBSR.SBSR_SOURCE,9,2)+substring(SBSR.SBSR_SOURCE,5,2)+substring(SBSR.SBSR_SOURCE,7,2))) <= '2019-02-28'
我在文件中有这两个参数,我希望脚本将日期更改为提前一个月并设置其最后一天。
到目前为止,日期为2019-02-28,我希望脚本查找日期并将其替换为2019-03-31。
我尝试了以下脚本,但结果不一样
PRM_FIL_DIR=$1 PRM_FIL_NME=$2 LOG_FIL_DIR=$3 LOG_FIL_NME=$4 echo Begining of the log File > $LOG_FIL_DIR/$LOG_FIL_NME From=`grep '$$mp_Custom_Filter_D_Prem=AND BLSB.BLSB_CREATE_DTM <' $PRM_FIL_DIR/$PRM_FIL_NME | awk -F= '{print $3}'|uniq| sed "s/'//g" | awk '$1=$1' ` echo $From is Date value found in $PRM_FIL_DIR/$PRM_FIL_NME parameter File >> $LOG_FIL_DIR/$LOG_FIL_NME To=`date -d "$From 2 month -1 day" +%Y-%m-%d` echo $To is the value to be replaced in $PRM_FIL_DIR/$PRM_FIL_NME parameter File >> $LOG_FIL_DIR/$LOG_FIL_NME From_FNL="'$From'" To_FNL="'$To'" sed -i "s/$From_FNL/$To_FNL/g" "$PRM_FIL_DIR/$PRM_FIL_NME" echo Date values replaced >> $LOG_FIL_DIR/$LOG_FIL_NME echo End of the log File >> $LOG_FIL_DIR/$LOG_FIL_NME PRM_FIL_DIR=$1 PRM_FIL_NME=$2 LOG_FIL_DIR=$3 LOG_FIL_NME=$4 echo Begining of the log File > $LOG_FIL_DIR/$LOG_FIL_NME From=`grep '$$mp_Custom_Filter_D_Prem=AND BLSB.BLSB_CREATE_DTM <' $PRM_FIL_DIR/$PRM_FIL_NME | awk -F= '{print $3}'|uniq| sed "s/'//g" | awk '$1=$1' ` echo $From is Date value found in $PRM_FIL_DIR/$PRM_FIL_NME parameter File >> $LOG_FIL_DIR/$LOG_FIL_NME To=`date -d "$From 2 month -1 day" +%Y-%m-%d` echo $To is the value to be replaced in $PRM_FIL_DIR/$PRM_FIL_NME parameter File >> $LOG_FIL_DIR/$LOG_FIL_NME From_FNL="'$From'" To_FNL="'$To'" sed -i "s/$From_FNL/$To_FNL/g" "$PRM_FIL_DIR/$PRM_FIL_NME" echo Date values replaced >> $LOG_FIL_DIR/$LOG_FIL_NME echo End of the log File >> $LOG_FIL_DIR/$LOG_FIL_NME
我希望输出为该月的最后一天,即2019-03-31,而实际输出为2019-03-29
答案 0 :(得分:1)
尝试此流程,
lastmonth=$( date -d "-$(date +%d) days" +%Y%m%d)
currentmonth=$( date -d "-$(date +%d) days month" +%Y%m%d)
sed "s/$lastmonth/$currentmonth/g" filename.txt