连字符和下划线在sed中不兼容 [英] Hyphen and underscore not compatible in sed
问题描述
我很难让sed识别其模式字符串中的连字符和下划线.
I'm having trouble getting sed to recognize both hyphen and underscore in its pattern string.
有人知道为什么吗
[a-z|A-Z|0-9|\-|_]
在以下示例中,
的作用类似于
in the following example works like
[a-z|A-Z|0-9|_]
?
$ cat /tmp/sed_undescore_hypen
lkjdaslf lkjlsadjfl dfpasdiuy service-type = service-1; jaldkfjlasdjflk address = address1; kldjfladsf
lkjdaslf lkjlsadjfl dfasdf service-type = service_1; jaldkfjlasdjflk address = address1; kldjfladsf
$ sed 's/.*\(service-type = [a-z|A-Z|0-9|\-|_]*\);.*\(address = .*\);.*/\1 \2/g' /tmp/sed_undescore_hypen
lkjdaslf lkjlsadjfl dfpasdiuy service-type = service-1; jaldkfjlasdjflk address = address1; kldjfladsf
service-type = service_1 address = address1
$ sed 's/.*\(service-type = [a-z|A-Z|0-9|\-]*\);.*\(address = .*\);.*/\1 \2/g' /tmp/sed_undescore_hypen
service-type = service-1 address = address1
lkjdaslf lkjlsadjfl dfasdf service-type = service_1; jaldkfjlasdjflk address = address1; kldjfladsf
$ sed 's/.*\(service-type = [a-z|A-Z|0-9|_]*\);.*\(address = .*\);.*/\1 \2/g' /tmp/sed_undescore_hypen
lkjdaslf lkjlsadjfl dfpasdiuy service-type = service-1; jaldkfjlasdjflk address = address1; kldjfladsf
service-type = service_1 address = address1
推荐答案
如前所述,您不需要任何内容即可在方括号表达式中分隔范围.要做的就是在表达式所匹配的字符上添加|
.
As mentioned, you don't need anything to separate your ranges in a bracket expression. All that will do is adding |
to the characters matched by the expression.
然后,要添加连字符,可以将其作为表达式中的第一个或最后一个字符:
Then, to add a hyphen, you can just put it as the first or last character in the expression:
[a-zA-Z0-9_-]
最后,根据您的语言环境,a-z
之类的范围不一定表示abcd...xyz
.您可以改用POSIX字符类:
And finally, ranges like a-z
don't necessarily mean abcd...xyz
, depending on your locale. You could use a POSIX character class instead:
[[:alnum:]_-]
其中[:alnum:]
对应于您的语言环境的所有字母数字字符.在C
语言环境中,它对应于0-9A-Za-z
.
Where [:alnum:]
corresponds to all alphanumeric characters of your locale. In the C
locale, it corresponds to 0-9A-Za-z
.
这篇关于连字符和下划线在sed中不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!