如果是重复,请原谅。我有crontab条目,如:
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
如何使用 sed 在包含“as.sh”调用的行上插入#? 如何取消注释?
答案 0 :(得分:5)
您可以使用:
sed '/\/as.sh/s/^/#/'
将替换包含/as.sh
的所有行的注释字符的起始行零宽度标记,如以下示例所示:
pax> echo '
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
' | sed '/\/as.sh/s/^/#/'
* * * * * sleep 15;/etc/opt/wer.sh
#1 * * * * /opt/sfm/qwe/as.sh
但你需要记住几件事。
cron
它需要重新读取它。如果您使用crontab
命令本身,则会自动执行此操作,但如果您直接编辑文件,则可能需要向cron
发送信号。要删除标记,请使用:
sed '/\/as.sh/s/^#//'
这是相反的操作,它会找到包含/as.sh
的行,并在行的开头替换任何#
字符。
答案 1 :(得分:3)
添加评论:
sed -e "s/\(.*\)\(as.sh\)/\#\1\2/g"
删除评论:
sed -e "s/\(^#\)\(.*\)\(as.sh\)/\2\3/g"
答案 2 :(得分:-2)
使用crontab -e修改当前用户的crontab。