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

前端 未结 5 534
旧时难觅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 '/<pattern>/s/^/#/g' file
    

    And to uncomment it:

    sed -i '/<pattern>/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.

    0 讨论(0)
  • 2021-01-30 03:35

    For mac, which doesn't support standard sed parameters, this would remove the hashtag

    sed -i "" "/.*#.*d\/docker-php-ext-xdebug\.ini.*/s/^#//g" docker-compose.yml
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-30 03:48

    Try this sed command,

    sed -i '/^#.* 2001 /s/^#//' file
    
    0 讨论(0)
  • 2021-01-30 03:54

    To complement @Avinash Raj's helpful answer with a more generic, POSIX-compliant solution.

    • Toggles commenting of lines that match a specifiable string that must occur as a separate word anywhere on the line.
    • The comment character (string) is also specifiable.

    Note that the solution is awk-based, because a robust portable solution with sed is virtually impossible due to the limitations of POSIX' basic regular expressions.

    awk -v commentId='#' -v word='2001' '
      $0 ~ "(^|[[:punct:][:space:]])" word "($|[[:punct:][:space:]])" { 
        if (match($0, "^[[:space:]]*" commentId))
          $0 = substr($0, RSTART + RLENGTH)
        else
          $0 = commentId $0
      } 
      { print }
      ' file > tmpfile.$$ && mv tmpfile.$$ file
    
    • (^|[[:punct:][:space:]]) and ($|[[:punct:][:space:]]) are the POSIX extended regex equivalents of the \< and \> word-boundary assertions known from other regex dialects.
    • Whitespace after the comment char is preserved, but not before it.
    • When prepending the comment char to a line, it is directly prepended, without whitespace.
    • Thus, if you only toggle comments with this solution, all whitespace is preserved.
    • POSIX awk doesn't offer in-place updating (neither does POSIX sed, incidentally), hence the output is first captured in a temporary file and that file then replaces the original on success.
    0 讨论(0)
提交回复
热议问题