6

I need to combine different character classes in a regular expresion used by sed. I need to match [:word:] and the minus symbol -. How does that look like? All my attempts failed at trying or searching a solution.

In the following strings I would like to match everything until the first space:

foo-bar     |
baz-xyz-123 |
Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
SpaceTrucker
  • 163
  • 1
  • 5

1 Answers1

5

POSIXly:

sed 's/[^[:alnum:]_-]//g'

will remove everything is not alpha numeric characters in your current locale, _ and -.

$ echo 'foo-bar     |' | sed -e 's/[^[:alnum:]_-]//g'
foo-bar

But if you want to print everything until first space:

sed -e 's/^\([^ ]*\) .*/\1/'

or awk:

awk '{print $1}'
cuonglm
  • 150,973
  • 38
  • 327
  • 406