Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyphen and underscore not compatible in sed

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
like image 376
techie11 Avatar asked Mar 12 '17 05:03

techie11


People also ask

How do you pass special characters in sed?

\ 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.

Does sed support regular expressions?

Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.

How do you match sed?

"[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.

How do you use sed command to replace a string in a file?

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.


1 Answers

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.

like image 132
Benjamin W. Avatar answered Sep 29 '22 04:09

Benjamin W.