仅尝试删除非 ASCII 字符 [英] Trying to delete non-ASCII characters only

查看:65
本文介绍了仅尝试删除非 ASCII 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理文本文件并从文本中删除非ASCII字符.我不想删除该行.我只想删除令人反感的字符.我正在尝试使以下表达式起作用:

I am trying to manipulate a text file and remove non-ASCII characters from the text. I don't want to remove the line. I only want to remove the offending characters. I am trying to get the following expression to work:

sed'/[\ x80- \ xFF]/d'

推荐答案

对于特定版本的sed,建议的解决方案可能会失败,例如GNU sed 4.2.1.

The suggested solutions may fail with specific version of sed, e.g. GNU sed 4.2.1.

使用 tr :

tr -cd '[:print:]' < yourfile.txt

这将删除所有未包含在 [\ x20- \ x7e] 中的字符.

This will remove any characters not in [\x20-\x7e].

如果您想保留例如换行,只需添加 \ n :

If you want to keep e.g. line feeds, just add \n:

tr -cd '[:print:]\n' < yourfile.txt

如果您确实要保留所有 个ASCII字符(甚至包括控制代码):

If you really want to keep all ASCII characters (even the control codes):

tr -cd '[:print:][:cntrl:]' < yourfile.txt

这将删除所有未包含在 [\ x00- \ x7f] 中的字符.

This will remove any characters not in [\x00-\x7f].

这篇关于仅尝试删除非 ASCII 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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