【问题标题】:Wider hexdump output更宽的 hexdump 输出
【发布时间】:2019-10-28 23:39:02
【问题描述】:

我非常喜欢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 命令是什么样子也会有所帮助。 (我能找到的文档对此没有帮助。)

【问题讨论】:

  • 您可以与od -tx1z -w file | sed -e 's/[>]/| /' -e 's/.$/ |/'亲密接触

标签: shell formatting hex hexdump


【解决方案1】:

我想你可能只需要拆分第二个-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\""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多