sed for sed的正则表达式更换

时间:2014-11-16 18:05:17

标签: regex bash sed

我想用shell和regex替换一行中的字符串。

例如,在文件config.php中,我想将TO_REPLACE替换为OK_REPLACED

home/TO_REPLACE/dmwebolabo;

我试过这个命令:

sed -i 's/^\(home\/\)[^\/]*\/dmwebolabo/\1OK_REPLACED\/dmwebolabo/g' config.php;

但它没有用。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

根据您在评论中的陈述:

Hello Thanks for all of you I see better now the problem When I put home/TO_REPLACE/dmwebolabo alone it works fine but when it's in its original line it doesn't This is the original line : define('UPLOAD_DIRECTORY', '' . DS . 'home/TO_REPLACE/dmwebolabo' . DS);

问题看起来像你的正则表达式:

^\(home\/\)[^\/]*\/dmwebolabo

^锚定到字符串的开头。尝试删除锚点。

\(home\/\)[^\/]*\/dmwebolabo