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|\-|_]
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
\ followed by a letter has a special meaning (special characters) in some implementations, and \ followed by some other character means \c or c depending on the implementation. With single quotes around the argument ( sed 's/…/…/' ), use '\'' to put a single quote in the replacement text.
Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.
"[0-9][0-9]*" matches one or more numbers. Another way to do this is to use the "+" meta-character and use the pattern "[0-9]+" as the "+" is a special character when using "extended regular expressions." Extended regular expressions have more power, but sed scripts that treated "+" as a normal character would break.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
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_-]
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:]_-]
Where [:alnum:]
corresponds to all alphanumeric characters of your locale. In the C
locale, it corresponds to 0-9A-Za-z
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With