shell脚本搜索并用日期替换sting值

时间:2016-06-26 21:15:43

标签: linux shell date sed ksh

脚本abc.ksh应该替换文件xyz.sql的值,如下所示

搜索字符串“%s_sitename截止日期”+%Y-%b-%e“”每当我们执行abc.ksh时,sysdate更改日期

s1='%s_sitename%[a-z][0-9]/-'
s2='%s_sitename%'"$(date "+%Y-%b-%e")"
sed -i 's/'"$s1"'/'"$s2"'/g' xyz.sql       

每次使用sysdate更新abc.sql文件

"%s_sitename% as of 2016-Jun-22"

随着电流的到来,我得到如下的有线输出

1 个答案:

答案 0 :(得分:0)

s1='%s_sitename% as of [a-zA-Z0-9-]*'
s2='%s_sitename% as of '"$(date "+%Y-%b-%e")"
sed -i 's/'"$s1"'/'"$s2"'/g' xyz.sql

测试:

echo "%s_sitename% as of 2005-Apr-02" | sed 's/'"$s1"'/'"$s2"'/g'

输出:

%s_sitename% as of 2016-Jun-26

请参阅:The Stack Overflow Regular Expressions FAQ