所以我使用 emacs,它有一个惊人的十六进制模式来查看文件中十六进制值的字节偏移量,类似于:
87654321 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789abcdeff
00000000: 5765 6c63 6f6d 6520 746f 2047 4e55 2045 Welcome to GNU E
作为这个能力的粉丝。想知道这是否是我可以在 xxd 或 hexdump 中提取的功能?或者如果有人有一个 awk 脚本可以执行此操作并使其保持正确排列
答案1
我最喜欢的用法hexdump
是这种格式:
hexdump -v -e '"%08_ax "' -e '16/1 "%02X "" "" "' -e '16/1 "%_p""\n"'
这给出了类似于的输出
% echo hello there everyone | hexdump -v -e '"%08_ax "' -e '16/1 "%02X "" "" "' -e '16/1 "%_p""\n"'
00000000 68 65 6C 6C 6F 20 74 68 65 72 65 20 65 76 65 72 hello there ever
00000010 79 6F 6E 65 0A yone.
很容易简单地echo
在前面放一个:
echo hello there everyone | (echo '87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef' ; hexdump -v -e '"%08_ax "' -e '16/1 "%02X "" "" "' -e '16/1 "%_p""\n"')
87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
00000000 68 65 6C 6C 6F 20 74 68 65 72 65 20 65 76 65 72 hello there ever
00000010 79 6F 6E 65 0A
或者,我们可以“分页”输出;例如,每 16 行放置一个标题,并使用一个简单的awk
过滤器:
cat x | hexdump -v -e '"%08_ax "' -e '16/1 "%02X "" "" "' -e '16/1 "%_p""\n"' | awk '(NR-1)%16 == 0 { print "\n87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef"} ; { print }' | less
87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
00000000 54 68 69 73 20 69 73 20 6C 69 6E 65 20 31 0A 54 This is line 1.T
00000010 68 69 73 20 69 73 20 6C 69 6E 65 20 32 0A 54 68 his is line 2.Th
00000020 69 73 20 69 73 20 6C 69 6E 65 20 33 0A 54 68 69 is is line 3.Thi
00000030 73 20 69 73 20 6C 69 6E 65 20 34 0A 54 68 69 73 s is line 4.This
00000040 20 69 73 20 6C 69 6E 65 20 35 0A 54 68 69 73 20 is line 5.This
00000050 69 73 20 6C 69 6E 65 20 36 0A 54 68 69 73 20 69 is line 6.This i
00000060 73 20 6C 69 6E 65 20 37 0A 54 68 69 73 20 69 73 s line 7.This is
00000070 20 6C 69 6E 65 20 38 0A 54 68 69 73 20 69 73 20 line 8.This is
00000080 6C 69 6E 65 20 39 0A 54 68 69 73 20 69 73 20 6C line 9.This is l
00000090 69 6E 65 20 31 30 0A 54 68 69 73 20 69 73 20 6C ine 10.This is l
000000a0 69 6E 65 20 31 31 0A 54 68 69 73 20 69 73 20 6C ine 11.This is l
000000b0 69 6E 65 20 31 32 0A 54 68 69 73 20 69 73 20 6C ine 12.This is l
000000c0 69 6E 65 20 31 33 0A 54 68 69 73 20 69 73 20 6C ine 13.This is l
000000d0 69 6E 65 20 31 34 0A 54 68 69 73 20 69 73 20 6C ine 14.This is l
000000e0 69 6E 65 20 31 35 0A 54 68 69 73 20 69 73 20 6C ine 15.This is l
000000f0 69 6E 65 20 31 36 0A 54 68 69 73 20 69 73 20 6C ine 16.This is l
87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
00000100 69 6E 65 20 31 37 0A 54 68 69 73 20 69 73 20 6C ine 17.This is l
00000110 69 6E 65 20 31 38 0A 54 68 69 73 20 69 73 20 6C ine 18.This is l
我可能想在其中放置一些分隔符,以便更容易区分“标题”和内容。
这很容易制作成脚本:
% cat hex
#!/bin/sh
hexdump -v -e '"%08_ax "' -e '16/1 "%02X "" "" "' -e '16/1 "%_p""\n"' | awk '(NR-1)%16 == 0 { print "\n87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef\n======== == == == == == == == == == == == == == == == == ================"} ; { print }'
现在你可以做
% hex < x
或者
% cat x | hex
以及类似的命令。
87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
======== == == == == == == == == == == == == == == == == ================
00000000 54 68 69 73 20 69 73 20 6C 69 6E 65 20 31 0A 54 This is line 1.T
00000010 68 69 73 20 69 73 20 6C 69 6E 65 20 32 0A 54 68 his is line 2.Th
00000020 69 73 20 69 73 20 6C 69 6E 65 20 33 0A 54 68 69 is is line 3.Thi
00000030 73 20 69 73 20 6C 69 6E 65 20 34 0A 54 68 69 73 s is line 4.This
00000040 20 69 73 20 6C 69 6E 65 20 35 0A 54 68 69 73 20 is line 5.This
00000050 69 73 20 6C 69 6E 65 20 36 0A 54 68 69 73 20 69 is line 6.This i
00000060 73 20 6C 69 6E 65 20 37 0A 54 68 69 73 20 69 73 s line 7.This is
00000070 20 6C 69 6E 65 20 38 0A 54 68 69 73 20 69 73 20 line 8.This is
00000080 6C 69 6E 65 20 39 0A 54 68 69 73 20 69 73 20 6C line 9.This is l
00000090 69 6E 65 20 31 30 0A 54 68 69 73 20 69 73 20 6C ine 10.This is l
000000a0 69 6E 65 20 31 31 0A 54 68 69 73 20 69 73 20 6C ine 11.This is l
000000b0 69 6E 65 20 31 32 0A 54 68 69 73 20 69 73 20 6C ine 12.This is l
000000c0 69 6E 65 20 31 33 0A 54 68 69 73 20 69 73 20 6C ine 13.This is l
000000d0 69 6E 65 20 31 34 0A 54 68 69 73 20 69 73 20 6C ine 14.This is l
000000e0 69 6E 65 20 31 35 0A 54 68 69 73 20 69 73 20 6C ine 15.This is l
000000f0 69 6E 65 20 31 36 0A 54 68 69 73 20 69 73 20 6C ine 16.This is l
87654321 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
======== == == == == == == == == == == == == == == == == ================
00000100 69 6E 65 20 31 37 0A 54 68 69 73 20 69 73 20 6C ine 17.This is l