Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 654
viertro Avatar asked Dec 05 '14 23:12

viertro


1 Answers

are you looking for this?

kent$ echo 'a + b + c - d - e'|sed 's/[^-+]//g'
++--
like image 85
Kent Avatar answered Sep 18 '22 08:09

Kent