-1
  • My text:mmhmmh here_is_you 05451ab8 888
  • My regex: mmhmmh \w* ([ab0-9]*) \d*
  • My sed command: echo "mmhmmh here_is_you 05451ab8 888" | sed -n 's/mmhmmh \w* \([ab0-9]*\) \d*/\1/p'
  • Result: 05451ab8888 instead of 05451ab8

Why doesn't sed respect my regex? I checked and my regex should correctly select the expected result.

Alexis
  • 2,136
  • 2
  • 19
  • 47
  • 2
    Possible duplicate of [Why doesn't \`\d\` work in regular expressions in sed?](https://stackoverflow.com/questions/14671293/why-doesnt-d-work-in-regular-expressions-in-sed) and [Can't match regex with sed](https://stackoverflow.com/q/39103215/3776858) – Cyrus Aug 16 '19 at 05:19
  • See also https://stackoverflow.com/questions/18514135/bash-regular-expression-cant-seem-to-match-s-s-etc – tripleee Aug 16 '19 at 05:28

1 Answers1

-1

As Cyrus points out with the comment link, \d does gives problems, use [0-9]

echo "mmhmmh here_is_you 05451ab8 888" | sed -n 's/mmhmmh [a-zA-Z0-9_]* \([ab0-9]*\) [0-9]*/\1/p'
05451ab8

Or [[:digit:]]

echo "mmhmmh here_is_you 05451ab8 888" | sed -n 's/mmhmmh [[:alnum:]_]* \([ab0-9]*\) [[:digit:]]*/\1/p'
05451ab8

And as trpleee points out, do not use \w either.

Jotne
  • 40,548
  • 12
  • 51
  • 55