Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all special characters in Linux text

vim pic How to remove the special characters shown as blue color in the picture 1 like: ^M, ^A, ^@, ^[. In my understanding, ^M is a windows newline character, I can use sed -i '/^M//g' to remove it, but it doesn't work to remove others. The command dos2unix doesn't work, neither. Are there exist any ways that I can use to remove them both?

like image 666
vinllen Avatar asked Mar 30 '17 04:03

vinllen


People also ask

How do you remove special and space characters from a string in Unix?

If you just want to remove certain characters I find the GNU version of tr easier to use, which supports a -d parameter to delete characters instead of translating them and also supports certain character classes. In this case just tr -d '[*][:space:]' might work well for you.


1 Answers

Remove everything except the printable characters (character class [:print:]), with sed:

sed $'s/[^[:print:]\t]//g' file.txt

[:print:] includes:

  • [:alnum:] (alpha-numerics)
  • [:punct:] (punctuations)
  • space

The ANSI C quoting ($'') is used for interpreting \t as literal tab inside $'' (in bash and alike).

like image 146
heemayl Avatar answered Oct 20 '22 14:10

heemayl