How to make a line as a comment in SED

我只是一个虾纸丫 提交于 2019-12-03 17:19:12

You can use:

sed '/\/as.sh/s/^/#/'

which will replace the start-line zero-width marker with a comment character for all lines containing /as.sh, as shown in the following example:

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

But you need to keep in mind a couple of things.

  • It's usually not enough to just change the file, you also need to notify cron that it needs to re-read it. This is automatic if you use the crontab command itself but you may have to send a signal to cron if you're editing the file directly.
  • It's not always a good idea to turn scripts loose on important system files. Make sure you know what you're doing and don't trust any old codger on the net (including me). Test it thoroughly.

To get rid of the marker, use:

sed '/\/as.sh/s/^#//'

This is the opposite operation, it finds those lines containing /as.sh and substitute any # character at the start of the line with nothing.

To add the comment:

sed -e "s/\(.*\)\(as.sh\)/\#\1\2/g"

To remove the comment:

sed -e "s/\(^#\)\(.*\)\(as.sh\)/\2\3/g"

use crontab -e to modify the current user's crontab.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!