Hyphen and underscore not compatible in sed

前端 未结 4 2016
傲寒
傲寒 2021-01-18 15:39

I\'m having trouble getting sed to recognize both hyphen and underscore in its pattern string.

Does anyone know why

[a-z|A-Z|0-9|\\-|_]
4条回答
  •  太阳男子
    2021-01-18 16:09

    $ sed 's/.*\(service-type = [a-z|A-Z|0-9|_-]*\);.*\(address = .*\);.*/\1    \2/g' sed_underscore_hypen.txt
    service-type = service-1    address = address1
    service-type = service_1    address = address1
    
    pknga_000@miro MINGW64 ~/Documents
    $ sed 's/.*\(service-type = [-a-z|A-Z|0-9|_]*\);.*\(address = .*\);.*/\1    \2/g' sed_underscore_hypen.txt
    service-type = service-1    address = address1
    service-type = service_1    address = address1
    

    To match a hyphen in a character class, it must not be placed between two characters, otherwise it will be interpreted as a range operator. So to match a hyphen, place it at the beginning or end of the character class: and no escaping is necessary. See this answer for explanation: https://stackoverflow.com/a/4068725

提交回复
热议问题