xxdにトップバーにバイトオフセットを表示させますか?

xxdにトップバーにバイトオフセットを表示させますか?

だから私は次のように、ファイル内の16進値のバイトオフセットを見ることができる素晴らしい16進モードを持つ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単に1つを前に置くのは簡単です。

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

おすすめ記事