SED/tr etc.. : How to comment out a line that contains “string” in a file?

前端 未结 2 1381
闹比i
闹比i 2020-12-06 08:21

my file contains lines such as these:

-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         


        
相关标签:
2条回答
  • 2020-12-06 08:59

    Through sed,

    sed '/ 2001 /s/^/#/' file
    

    Add inline edit -i option to save the changes made.

    sed -i '/ 2001 /s/^/#/' file
    

    Example:

    $ sed '/ 2001 /s/^/#/' 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 INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT
    
    0 讨论(0)
  • 2020-12-06 09:09

    This awk would do too.

    awk '/2001/ {$0="#"$0}1' 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 INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT
    
    0 讨论(0)
提交回复
热议问题