2012-01-20 42 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' 

這可以避免轉義'/'字符。