连字符和下划线在 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|-|_]
在下面的例子中就像
[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 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屋!