Replace Strings Using Sed And Regex

前端 未结 6 1929
猫巷女王i
猫巷女王i 2020-12-28 14:57

I\'m trying to uncomment file content using sed but with regex (for example: [0-9]{1,5})

# one two 12
# three four 34
# five six 56

The fol

6条回答
  •  旧时难觅i
    2020-12-28 15:23

    For complying sample question, simply

    sed 's/^# //' file
    

    will suffice, but if there is a need to remove the comment only on some lines containing a particular regex, then you could use conditionnal address:

    sed '/regex/s/^# //' file
    

    So every lines containing regex will be uncomented (if line begin with a #)

    ... where regex could be [0-9] as:

    sed '/[0-9]/s/^# //' file
    

    will remove # at begin of every lines containing a number, or

    sed '/[0-9]/s/^# \?//' file
    

    to make first space not needed: #one two 12, or even

    sed '/[0-9]$/s/^# //' file
    

    will remove # at begin of lines containing a number as last character. Then

    sed '/12$/s/^# //' file
    

    will remove # at begin of lines ended by 12. Or

    sed '/\b\(two\|three\)\b/s/^# //' file
    

    will remove # at begin of lines containing word two or three.

提交回复
热议问题