如何从hexdump中仅打印十六进制值而不包括行号或ASCII表?

53

参考 Convert decimal to hexadecimal in UNIX shell script,我正在尝试从hexdump中仅打印出16进制值,即不要打印行号和ASCII表。

但是以下命令没有输出:

hexdump -n 50 -Cs 10 file.bin |  awk '{for(i=NF-17; i>2; --i) print $i}'

年龄并不是主要因素,而是赞同和答案质量;-) http://meta.stackoverflow.com/questions/251938/should-i-flag-a-question-as-duplicate-if-it-has-received-better-answers - Ciro Santilli OurBigBook.com
hexdump file.bin | sed "s/[^ ]* //1" 这将删除行号,默认情况下不会打印ASCII表。 - Owl
4个回答

86

使用xxd更适合这项工作:

xxd -p -l 50 -seek 10 file.bin

来自man xxd

xxd - make a hexdump or do the reverse.

    -p | -ps | -postscript | -plain
        output in postscript continuous hexdump style. Also known as plain hexdump style.

    -l len | -len len
        stop after writing <len> octets.
 
    -seek offset
        When used after -r: revert with <offset> added to file positions found in hexdump.

9
hexdump 工具包含在 busybox 中,因此可用于嵌入式系统,而 xxd 则不行。 - Hi-Angel

67

您可以指定hexdump输出的确切格式,但这有点棘手。以下是默认输出(不包括文件偏移量):

hexdump -e '16/1 "%02x " "\n"' file.bin

对我来说,看起来这会在每行末尾产生额外的空格,但出于某种原因它没有。


太好了!这个答案对我很有用!但是我认为还是加上-C选项更好。如果你再加上cut -c 9- | head -n 1,你的输出将只显示十六进制数字。 - Kyrol
这篇博客文章展示了更多如何修改hexdump输出格式的示例。链接:https://blog.senpaisilver.com/linux/formatting-hexdumps-output/ - Matthias Braun
3
在列表末尾添加-v参数,这样每一行都会被列出来,不会使用*将相同的行压缩。 - dagelf

30
作为替代方案,请考虑使用xxd -p file.bin

4

首先,删除发射ascii信息的-C

然后,您可以使用以下命令删除偏移量:

hexdump -n 50 -s 10 file.bin | cut -c 9-

1
它似乎可以工作,但改变了字节序… - 0x90

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接