2

So Im using emacs which has a stupendous hexl-mode to view the byte offset in a file right over the hex values similar to:

87654321  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789abcdeff             
00000000: 5765 6c63 6f6d 6520 746f 2047 4e55 2045  Welcome to GNU E

As a fan of this capability. Wondering if this is a capability I can pull out of in the xxd or hexdump? Or if anybody has an awk script that does this and keeps it lined up properly

3

My favourite use of hexdump is in this format:

hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"'

That gives output similar to

% 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.

It would be easy to simply put an echo in front of this:

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 

Alternatively we could "page" the output; eg put the header every 16 lines, with a simple awk filter:

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

I might want to put some separators in there to make it easier to distinguish between the "header" and the content.

This is easily made into a script:

% 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 }'

Now you can do

% hex < x

or

% cat x | hex

And similar commands.

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
|improve this answer|||||
  • Thank you for the answer. This seems pretty impressive work. Way more sophisticated than the cheap way I made after asking this question of a bash function just echos out the header literally ;D. I will definitely test this bad boy out and probably replace it with this script as Im digging the pager aspect – LFMekz Jul 6 '19 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.