我想使一个文件包括不可打印字符,只包括可打印字符。我认为这个问题与ACSCII control action有关,但我找不到解决方法,也无法理解.[16D
(ASCII控制操作字符??)的含义在下面的文件中。在
输入文件的十六进制转储:
00000000: 4845 4c4c 4f20 5448 4953 2049 5320 5448 HELLO THIS IS TH
00000010: 4520 5445 5354 1b5b 3136 4420 2020 2020 E TEST.[16D
00000020: 2020 2020 2020 2020 2020 201b 5b31 3644 .[16D
00000030: 2020
当我cat
在bash
上编辑了那个文件,我只得到了一个消息:“你好”。我认为这是因为默认的cat
解释了ASCII控制操作,两个.[16D
为什么两个.[16D
字符串组成cat
文件只是为了打印“HELLO”?,还有。。。如何使该文件仅包含可打印字符,即“HELLO”?在
我想到的最简单的解决方案是
如果这仍然没有帮助,那么尝试也包括uni代码特定的[诅咒.ascii]在
hexdump显示
.[16D
中的点实际上是转义符,\x1b
。Esc[
nD
是删除n
字符的ANSI escape code。因此Esc[16D
告诉终端删除16个字符,这解释了cat
输出。在从文件中删除ANSI转义码有多种方法,可以使用Bash命令(例如使用
sed
,如Anubhava的回答)或Python。在但是,在这种情况下,最好通过终端仿真器运行该文件,以解释文件中任何现有的编辑控制序列,这样在应用这些编辑序列之后,您就可以得到文件作者想要的结果。在
在Python中实现这一点的一种方法是使用pyte,这是一个Python模块,它实现了一个简单的与VTXXX兼容的终端仿真器。您可以使用
pip
轻松地安装它,下面是它在readthedocs上的文档。在下面是一个简单的演示程序,它解释问题中给出的数据。它是为python2编写的,但是很容易适应python3。
pyte
支持Unicode,它的标准流类需要Unicode字符串,但是这个示例使用了ByteStream,所以我可以向它传递一个纯字节字符串。在输出
^{pr2}$输出的十六进制转储
您可以尝试使用
sed
命令从文件中删除所有不可打印的字符:相关问题 更多 >
编程相关推荐