2

I want to replace all symbols like '.','-','/' and so on by ' '

I triedsed 's/[[!:alnum:]]/ /g' example.txt but it does not work

and for sed 's/[,;\.\-\/()_]/ /g' example.txt, I found that I cannot replace '-' by ' '.

is there any way to do that?

5

To match all non-alphanumerics, use [^[:alnum:]]. It's similar to [^abc]: the leading caret makes the bracket expression match all but the listed characters.

To match a literal dash, you need to make sure it's not interpreted as creating a list of characters to match. Make it the first character of the bracket expression: [-,.;/()_].

Note that sed doesn't take the / as a separator for s/// if it's within a bracket expression, so there's no need to put a backslash in front of it. (The backslash will match itself.) E.g. echo 'a\b/c' | sed 's/[/\]/./g' prints a.b.c.

See for example the regex(7) man page for a description of the regular expression syntax. (Note that it mainly discusses extended regular expressions (ERE), used by sed -E in many/most seds.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.