Columns

讓 xxd 顯示最上面一列的字節偏移量?

  • July 6, 2019

所以我使用具有驚人的十六進制模式的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 腳本來執行此操作並使其正確排列

我最喜歡使用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

引用自:https://unix.stackexchange.com/questions/528676