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?
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.
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)The ANSI C quoting ($'') is used for interpreting \t as literal tab inside $'' (in bash and alike).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With