如何在SED中将行作为注释 [英] How to make a line as a comment in SED

查看:154
本文介绍了如何在SED中将行作为注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是重复,请原谅。我有crontab条目,如下所示:

  * * * * * sleep 15; /etc/opt/wer.sh 
1 * * * * /opt/sfm/qwe/as.sh

#在包含使用 sed 调用as.sh的行上?

如何取消注释?

解决方案

您可以使用:

  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 的那些行,并替换任何字符在行的开头没有任何内容。


    Excuse me if it is a repeat. I have crontab entries which look like:

    *   *    *    *    *   sleep 15;/etc/opt/wer.sh
    1 * * * * /opt/sfm/qwe/as.sh
    

    How to insert a # on the line which contains a call to "as.sh" using sed?
    How to uncomment it back?

    解决方案

    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.

    这篇关于如何在SED中将行作为注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆