在 UNIX 中识别和删除空字符 [英] Identifying and removing null characters in UNIX

查看:56
本文介绍了在 UNIX 中识别和删除空字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不需要的空字符(ASCII NUL、\0)的文本文件.当我尝试在 vi 中查看它时,我看到 ^@ 符号,在普通文本中交错.我该怎么办:

I have a text file containing unwanted null characters (ASCII NUL, \0). When I try to view it in vi I see ^@ symbols, interleaved in normal text. How can I:

  1. 确定文件中哪些行包含空字符?我已经尝试对 \0\x0 进行 grepping,但这不起作用.

  1. Identify which lines in the file contain null characters? I have tried grepping for \0 and \x0, but this did not work.

删除空字符?在文件上运行 strings 清理了它,但我只是想知道这是否是最好的方法?

Remove the null characters? Running strings on the file cleaned it up, but I'm just wondering if this is the best way?

推荐答案

我会使用 tr:

tr < file-with-nulls -d '\000' > file-without-nulls

如果您想知道命令参数中间的输入重定向是否有效,它确实有效.实际上,大多数 shell 都会识别并处理命令行中任何位置的 I/O 重定向(<>、...).

If you are wondering if input redirection in the middle of the command arguments works, it does. Most shells will recognize and deal with I/O redirection (<, >, …) anywhere in the command line, actually.

这篇关于在 UNIX 中识别和删除空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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