How to remove all special characters in Linux text

≡放荡痞女 提交于 2019-11-30 06:59:29

问题


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?


回答1:


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




回答2:


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



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/43108359/how-to-remove-all-special-characters-in-linux-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!