sed操作无法按预期工作

时间:2015-09-02 14:13:46

标签: regex sed

我正在尝试这种sed操作并且没有发生

sed -e 's/\<\* \* \* \* \* \/opt\/scripts\/centralizedLogging\>/\<0 0 \* \* \* \/opt\/scripts\/centralizedLogging\>/g' /etc/cron.d/jobs

另一方面,这是有效的:

sed -e 's/\<0 0 \* \* \* \/opt\/scripts\/centralizedLogging\>/\<\* \* \* \* \* \/opt\/scripts\/centralizedLogging\>/g' /etc/cron.d/jobs

1 个答案:

答案 0 :(得分:0)

sed -e 's#<\([*][[:blank:]]\{1,\}\)\{2\}\(\([*][[:blank:]]\{1,\}\)\{3\}/opt/scripts/centralizedLogging>\)#<0 0 \2#g' /etc/cron.d/jobs
  • 使用[[:blank:]]\{1,\}代替更加通用,[*]更容易捕捉而不是含糊不清(阅读时)\*
  • 使用小组和出现计数器
  • 因使用路径而使用#作为分隔符而非传统/
  • 尽可能使用posix书写便携式

我强烈建议在cron作业或同化

上进行此类操作时使用备份文件