如何删除Linux文本中的所有特殊字符 [英] How to remove all special characters in Linux text

查看:1630
本文介绍了如何删除Linux文本中的所有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除图片 1 中显示为蓝色的特殊字符,例如:^ M, ^ A,^ @,^ [.以我的理解,^ M是Windows的换行符,我可以使用sed -i '/^M//g'来删除它,但不能删除其他人.命令dos2unix无效,也无效.有什么办法可以将它们都删除?

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?

推荐答案

使用sed删除除可打印字符(字符类[:print:])以外的所有内容:

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

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

[:print:]包括:

  • [:alnum:](字母数字)
  • [:punct:](标点符号)
  • 空间
  • [:alnum:] (alpha-numerics)
  • [:punct:] (punctuations)
  • space

ANSI C引号($'')用于将\t解释为$''中的文字制表符(在bash中).

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

这篇关于如何删除Linux文本中的所有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆