viertro
viertro

Reputation: 173

How to escape minus in regular expression with sed?

I need to free a string from unwanted characters. In this example I want to filter all +'s and all -'s from b and write the result to c. So if b is +fdd-dfdf+, c should be +-+.

read b
c=$(echo $b | sed 's/[^(\+|\-)]//g')

But when i run the script, the console says:

sed: -e expression #1, char 15: Invalid range end

The reason is the \- in my regular expression. How can I solve this problem and say, that I want to filter all -'s?

Upvotes: 5

Views: 3366

Answers (1)

Kent
Kent

Reputation: 195049

are you looking for this?

kent$ echo 'a + b + c - d - e'|sed 's/[^-+]//g'
++--

Upvotes: 6

Related Questions