我有一个配置文件xml
<tflow name="CENTRE" inputDTD="/JOBS/cnav/etc/jobReporting/batch/dtd/dtd-ContactCentre.dtd" inputFile="/JOBS/cnav/etc/jobReporting/import/2010.05.02.CONTACTCENTRE.xml" logPath="/JOBS/cnav/etc/jobReporting/logs/" rejectPath="/JOBS/cnav/etc/jobReporting/rejets/"/> <tflow name="SKILL" inputDTD="/JOBS/cnav/etc/jobReporting/batch/dtd/dtd-Skill.dtd" inputFile="/JOBS/cnav/etc/jobReporting/import/2010.05.02.SKILLS.xml" logPath="/JOBS/cnav/etc/jobReporting/logs/" rejectPath="/JOBS/cnav/etc/jobReporting/rejets/"/>
我的shell旨在改变,例如'2010.05.02.SKILLS.xml'和'newdate.SKILLS.xml'
目前我想起了SED,我写道:
sed 's/(import\/)(\d{4}.\d{2}.\d{2})/$1$newdate/g' myfile.xml
它不起作用,我使用RegExr(一个网站)测试模式,这很好。
是合成SED的问题吗? 感谢。
答案 0 :(得分:2)
默认情况下,大多数sed实现不支持扩展的regexp,因此您需要使用基本的regexp。这意味着您需要在括号和大括号之前添加反斜杠,并且不能使用\d
类。您还有反向引用的语法错误 - 它应该是\1
,而不是$1$
。所以,它应该是这样的:
sed 's/\(import\/\)\([0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}\)/\1newdate/' myfile.xml
答案 1 :(得分:1)
根据您的sed实现,您可能必须指定使用正则表达式。您使用的是什么操作系统/版本的sed?例如,您可能必须使用-E参数。
答案 2 :(得分:1)
周围有很多正则表达式方言......我认为 sed 不理解\d
。此外,使用\1
进行bactracking,如果$newdate
是shell变量,则使用双引号。并尝试-r
使用扩展的正则表达式。
尝试这样的事情
sed -r "s/(import\/)([0-9]{4}\.[0-9]{2}\.[0-9]{2})/\1$newdate/g" myfile.xml