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?
How to remove all special characters in Linux text
137.3k Views Asked by vinllen At
4
There are 4 best solutions below
0
On
Try this inside vi or vim:
or:
sed -e "s/^M//" filename > newfilename
Important: To enter ^M, type CTRL-V, then CTRL-M
2
On
To ensure that the command works with limited scope in Sed, force use of the "C" (POSIX) character classifications to avoid unpredictable behavior with non-ASCII characters:
LC_ALL=C sed 's/[^[:blank:][:print:]]//g' file.txt
Remove everything except the printable characters (character class
[:print:]), withsed:[:print:]includes:[:alnum:](alpha-numerics)[:punct:](punctuations)The ANSI C quoting (
$'') is used for interpreting\tas literal tab inside$''(inbashand alike).