regular expression in sed replace question mark

Viewed 219

I would like to replace all non-printable char and space and question mark to nothing.

sed 's/[^[:print:]\|?\| \r\t]//g'

but this will only replace non-printable char. The space and the question mark remains.

1 Answers

A pipe in the bracket expression means a pipe, not an alternation operator.

You can use

sed -E 's/[^[:print:]]|[[:blank:]?]//g' file > outfile

Here,

  • -E enables the POSIX ERE syntax
  • [^[:print:]] - any non-printable char
  • | - or (here, due to -E and the fact it is outside a bracket expression, it is an alternation operator [[:blank:]?] - a horizontal whitesapce or question mark.

You may chain two commands if you want to use POSIX BRE:

sed 's/[^[:print:]]//g;s/[[:blank:]?]//g' file > outfile
Related