简体   繁体   中英

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?

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).

要确保命令在Sed中使用有限的范围,强制使用“C”(POSIX)字符分类以避免使用非ASCII字符的不可预测的行为:

LC_ALL=C sed 's/[^[:blank:][:print:]]//g' file.txt

Try running below command on linux command prompt

Option - 1: (If dos2unix command is installed on Linux machine)

dos2unix sample_file.txt

Option - 2:

cat sample_file.txt | tr -d '\015' > new_sample_file.txt

Try this inside vi or vim:

[in ESC mode] type: :%s/^M//g

or:

sed -e "s/^M//" filename > newfilename

Important : To enter ^M, type CTRL-V, then CTRL-M

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM