How to UNCOMMENT a line that contains a specific string using Sed?

前端 未结 5 641
旧时难觅i
旧时难觅i 2021-01-30 03:11

The lines in the file :

-A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
-A          


        
5条回答
  •  旧时难觅i
    2021-01-30 03:37

    Quick example of how to comment and uncomment a line in a file.

    Sample file :

    umask 027
    TMOUT=600
    

    Lets now backup the file (just for laughs) and comment out and un comment:

    # backup file (because we should always do this)
    cp /etc/bash.bashrc /etc/bash.bashrc.$(date '+%Y-%m-%d,%H:%M:%S')
    
    # original: TMOUT=600   , result :# TMOUT=600
    sed -i '/[^#]/ s/\(^TMOUT=600.*$\)/#\ \1/' /etc/bash.bashrc
    
    # original # TMOUT=600   ,result :TMOUT=600
    sed -i '/^#.*TMOUT=600.*$/s/^#\ //' /etc/bash.bashrc
    

提交回复
热议问题