Linux 使用 hexdump 仅输出 ASCII

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13284335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:44:01  来源:igfitidea点击:

Using hexdump to output only ASCII

linuxcommand-linehexdump

提问by recluze

I'm trying to output ASCII values corresponding to some binary data. I have successfully applied the hexdumputility to output hexdump and ASCII side-by-side as below:

我正在尝试输出与某些二进制数据相对应的 ASCII 值。我已成功应用该hexdump实用程序来并排输出 hexdump 和 ASCII,如下所示:

00000120  20 20 20 20 3d 20 30 78  30 30 30 30 30 30 33 30  |    = 0x00000030|
00000130  0a 01 00 00 00 23 00 00  00 75 75 69 64 30 20 20  |.....#...uuid0  |
00000140  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 3d  |               =|
00000150  20 30 78 39 30 38 32 61  63 35 61 0a 01 00 00 00  | 0x9082ac5a.....|
00000160  23 00 00 00 75 75 69 64  31 20 20 20 20 20 20 20  |#...uuid1       |
00000170  20 20 20 20 20 20 20 20  20 20 3d 20 30 78 37 34  |          = 0x74|
00000180  61 37 34 37 36 66 0a 01  00 00 00 23 00 00 00 75  |a7476f.....#...u|
00000190  75 69 64 32 20 20 20 20  20 20 20 20 20 20 20 20  |uid2            |
000001a0  20 20 20 20 20 3d 20 30  78 61 32 35 35 35 63 30  |     = 0xa2555c0|

However, I would like to see only the ASCII as output values. I'm not interested in the hex values. For example, the output should be the following (approx. corresponding to the above):

但是,我只想看到 ASCII 作为输出值。我对十六进制值不感兴趣。例如,输出应如下所示(大约对应于上述内容):

= 0x00000030.....#...
uuid0=0x9082ac5a.....
uuid1=0x74a7476f

(I haven't been able to use the switches of hdfor this.)

(我一直无法为此使用 的开关hd。)

采纳答案by raina77ow

If you only need to see the text contents of binary file, stringsshould be useful:

如果您只需要查看二进制文件的文本内容,字符串应该很有用:

For each file given, GNU stringsprints the printable character sequences that are at least 4 characters long (or the number given with the -noption) and are followed by an unprintable character. stringsis mainly useful for determining the contents of non-text files.

对于给定的每个文件,GNUstrings打印至少 4 个字符长(或-n选项给出的数字)的可打印字符序列,后跟一个不可打印字符。 strings主要用于确定非文本文件的内容。

回答by iabdalkader

you can use od:

你可以使用od

od -t c file

Or awkwith hexdumplike so:

awkhexdump像这样:

hexdump -C file | awk '{for(i=NF; i>17; --i) print $i}'

Note: you have to change the awk command if you change the number of columns in hexdump.

注意:如果更改 hexdump 中的列数,则必须更改 awk 命令。

回答by RBerteig

While the answer from raina77owabout the strings(1)command is the correct way to get the result you actually wanted, the specific request to leverage hexdump(1)to filter out only printable characters may make sense in some contexts. I'll respond to that specifically here.

虽然来自raina77ow 的关于该strings(1)命令的答案是获得您真正想要的结果的正确方法,但hexdump(1)在某些情况下,利用过滤掉仅可打印字符的特定请求可能是有意义的。我会在这里专门回答这个问题。

The hexdump utility turns out to support a startlingly generalized formatting engine. This was probably done to make the implementation of the various selectable formats more uniform. If your copy of hexdump exposes that engine (as many do) through the -ecommand line option, then you can actually have it do what you asked for.

hexdump 实用程序支持一个惊人的通用格式引擎。这样做可能是为了使各种可选格式的实现更加统一。如果您的 hexdump 副本通过-e命令行选项公开了该引擎(很多都这样做),那么您实际上可以让它按照您的要求执行。

The key is the -eoption and the formatting language it supports. That language allows the specification of format strings that consume bytes of the input and produce text. A command such as:

关键是-e它支持的选项和格式语言。该语言允许指定使用输入字节并生成文本的格式字符串。一个命令,例如:

$ hexdump -e "16 \"%_p\" \"\n\"" hexdump.exe | head -16

will consume 16 bytes at a time, display them via the %_pformat, and add a newline after each 16th byte. Each non-printable character is replaced with a .in the output.

将一次消耗 16 个字节,通过%_p格式显示它们,并在每个第 16 个字节后添加一个换行符。每个不可打印的字符.在输出中被替换为 a 。

Other character-oriented formats to consider are %_cand %_u. The first replaces non-printing character with their ANSI-C escape sequence, or with a three digit octal number. The second replaces non-printing each character with the conventional name of the ASCII control character, or by a two-digit hexadecimal number.

其他要考虑的面向字符的格式是%_c%_u。第一个用 ANSI-C 转义序列或三位八进制数替换非打印字符。第二个使用 ASCII 控制字符的常规名称或两位十六进制数替换非打印每个字符。

If your copy of hexdumplacks the -eoption, or is slow, or you lack an implementation of hexdumpat all, then the liberally licensed, fast, and reasonably portable implementation of hexdumprecently released by William Ahernis worth looking at. It should build for lots of Unix-like systems out of the box, and with only minor tweaks it builds with MingW GCC on Windows. A key attribute of this implementation is that the single source file can be built as a shared library for inclusion in another program, as a Lua module for use from Lua, as well as a standalone executable implementing the hexdumpcommand.

如果您的副本hexdump缺少-e选项,或者速度很慢,或者根本没有实现hexdump,那么William Ahernhexdump最近发布的自由许可、快速且合理可移植的实现值得一看。它应该为许多开箱即用的类 Unix 系统构建,并且只需稍作调整即可在 Windows 上使用 MingW GCC 构建。该实现方式的一个关键属性是单个源文件可以被构建为包含在其他程序的共享库,作为一个Lua模块,用于从使用的Lua,以及一个独立的可执行文件执行hexdump命令。

回答by Ahtisham Wani

hexdump -C file /dev/sda4|cut -c 62-77

Hope it helps

希望能帮助到你