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

前端 未结 5 547
旧时难觅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条回答
  •  滥情空心
    2021-01-30 03:34

    Yes, to comment line containing specific string with sed, simply do:

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

    And to uncomment it:

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

    In your case:

    sed -i '/2001/s/^/#/g' file    (to comment out)
    sed -i '/2001/s/^#//g' file    (to uncomment)
    

    Option "g" at the end means global change. If you want to change only a single instance of pattern, just skip this.

提交回复
热议问题