2012-01-20 107 views
1

我想用sed从我的crontab文件中删除一行。首先,我将用户的crontab保存在文件text.txt中。然后我想删除这一行:Sed操纵crontab的行

*/5 * * * * svn update /root/tamainut/schedule/generico.sh && sh /root/tamainut/schedule/generico.sh 

我认为问题是与字符,但我迷路了。我想:

sed '*/5 * * * * svn update \/root\/tamainut\/schedule\/generico.sh && sh \/root\/tamainut\/schedule\/generico.sh/d' text.txt 
sed: -e expresión #1, carácter 1: unknown command: `*' 

我想:

sed '\*/5 \* \* \* \* svn update \/root\/tamainut\/schedule\/generico.sh && sh \/root\/tamainut\/schedule\/generico.sh/d' text.txt 
sed: -e expresión #1, carácter 115: unterminated address regex 

我要去哪里错了?

回答

3

*意味着 “重复任意次” 在sed =>使用\

sed '/\*\/5 \* \* \* \* svn update \/root\/tamainut\/schedule\/generico.sh && sh \/root\/tamainut\/schedule\/generico.sh/d' text.txt 

您也可以替代由.所有*/(这是不太可靠,但更舒适写的:它是我经常做的):

sed '/..5 . . . . svn update .root.tamainut.schedule.generico.sh && sh .root.tamainut.schedule.generico.sh/d' text.txt 
1

您可以使用:

sed '/\*\/5 \* \* \* \* svn update \/root\/tamainut\/schedule\/generico.sh && sh \/root\/tamainut\/schedule\/generico.sh/d' text.txt 

但是我的一个关键字,或几个关键字的挑选,使该行独特的,并使用它们:

sed '/generico.sh.*generico.sh/d' text.txt 

你原来遇到了问题,没有开放/启动正则表达式,并且*字符是元字符,所以它们也必须被转义。

1

如果您可以限制搜索字符串,它会清理sed代码。如果您必须匹配整行,请在地址范围上使用不同的分隔符:

 
$ sed '\|^\*/5 \* \* \* \* svn update /root/tamainut/schedule/generico.sh && sh /root/xxxx/file.sh$|d' 

这可以避免转义'/'字符。