我想使用sed
从我的crontab文件中删除一行。首先,我将用户的crontab保存在文件text.txt
中。然后我想删除这一行:
*/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
我哪里错了?
答案 0 :(得分: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 :(得分: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
您的原始文件遇到问题,没有打开/
以启动正则表达式,*
字符是元字符,因此它们也必须进行转义。
答案 2 :(得分:1)
如果您可以限制搜索字符串,它将清理sed代码。如果必须匹配整行,请在地址范围内使用不同的分隔符:
$ sed '\|^\*/5 \* \* \* \* svn update /root/tamainut/schedule/generico.sh && sh /root/xxxx/file.sh$|d'
这可以让您避免逃避' /'字符。