更宽的 hexdump 输出

Wider hexdump output

我非常喜欢 hd 命令的默认格式。例如:

$ head -c128 /bin/bash |hd
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  03 00 3e 00 01 00 00 00  30 f6 02 00 00 00 00 00  |..>.....0.......|
00000020  40 00 00 00 00 00 00 00  48 ce 11 00 00 00 00 00  |@.......H.......|
00000030  00 00 00 00 40 00 38 00  0b 00 40 00 1d 00 1c 00  |....@.8...@.....|
00000040  06 00 00 00 04 00 00 00  40 00 00 00 00 00 00 00  |........@.......|
00000050  40 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |@.......@.......|
00000060  68 02 00 00 00 00 00 00  68 02 00 00 00 00 00 00  |h.......h.......|
00000070  08 00 00 00 00 00 00 00  03 00 00 00 04 00 00 00  |................|
00000080

我正在寻找一个 hexdump 命令,它可以做同样的事情但是是双倍宽度的。输出应该类似于:

$ head -c128 /bin/bash |2hd
00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  03 00 3e 00 01 00 00 00  30 f6 02 00 00 00 00 00  |.ELF............| |..>.....0.......|
00000020  40 00 00 00 00 00 00 00  48 ce 11 00 00 00 00 00  00 00 00 00 40 00 38 00  0b 00 40 00 1d 00 1c 00  |@.......H.......| |....@.8...@.....|
00000040  06 00 00 00 04 00 00 00  40 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |........@.......| |@.......@.......|
00000060  68 02 00 00 00 00 00 00  68 02 00 00 00 00 00 00  08 00 00 00 00 00 00 00  03 00 00 00 04 00 00 00  |h.......h.......| |................|
00000080

到目前为止,我知道了。它没有正确排列。

2hd() {
  local poe='"  " 8/1 "%02x "'  # pieces of eight, heh
  hexdump -e '"%07.7_Ax\n"' \
          -e '"%07.7_ax" '"$poe $poe $poe $poe"' "  |" 32/1 "%_p" "|\n"' "$@"
}
$ head -c128 /bin/bash |2hd
0000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  03 00 3e 00 01 00 00 00  30 f6 02 00 00 00 00 00  |@.......H...........@.8...@.....|
0000040  06 00 00 00 04 00 00 00  40 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |h.......h.......................|
0000080  a8 02 00 00 00 00 00 00  a8 02 00 00 00 00 00 00  a8 02 00 00 00 00 00 00  1c 00 00 00 00 00 00 00  |................................|
00000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  98 cd 02 00 00 00 00 00  98 cd 02 00 00 00 00 00  |................................|
0000100

(我还没有决定是要右侧显示是一部分还是两部分。)

我希望在对 hexdump 的单一引用中完全做到这一点。了解获取 16-col hd 输出的 hexdump 命令的外观也会有所帮助。 (我能找到的文档对此没有帮助。)

我想你可能只需要拆分第二个 -e:

2hd() {
  local poe='"  " 8/1 "%02x "'
  hexdump -e '"%07.7_Ax\n"' \
          -e '"%07.7_ax" '"$poe $poe $poe $poe" \
          -e ' "  |" 32/1 "%_p" "|\n"' "$@"
}

多个 -e 每个都处理相同的输入。在您的原件中,%_p 适用于 之后的输入 %x,因为它在同一个 -e.


busybox hexdump source-C 定义为:

bb_dump_add(dumper, "\"%08.8_Ax\n\""); // final address line after dump
//------------------- "address  "   8 * "xx "    "  "  8 * "xx "
bb_dump_add(dumper, "\"%08.8_ax  \"8/1 \"%02x \"\"  \"8/1 \"%02x \"");
//------------------- "  |ASCII...........|\n"
bb_dump_add(dumper, "\"  |\"16/1 \"%_p\"\"|\n\"");

这意味着您可以将 hd 实现为:

hexdump -e "\"%08.8_Ax\n\"" -e "\"%08.8_ax  \"8/1 \"%02x \"\"  \"8/1 \"%02x \"" \
        -e "\"  |\"16/1 \"%_p\"\"|\n\""