How can I change the number of columns printed by `hexdump`?
Asked Answered
K

1

11

How can I change the number of columns printed by hexdump from the default 16 (to 21)?

Or where can I find the place to change the default format string used in hexdump in order to modify the number used there?

Kenyon answered 1/11, 2014 at 22:48 Comment(2)
You can try this hexdump -e '21/1 "%02x " "\n"'Clifton
Please find an answer that also contains information about the default format.Clifton
C
13

It seems that the default format is obtained thus:

hexdump -e '"%07.7_Ax\n"' -e '"%07.7_ax " 8/2 "%04x " "\n"'

From man hexdump:

 Implement the -x option:

       "%07.7_Ax\n"
       "%07.7_ax  " 8/2 "%04x " "\n"

If you want to understand hexdump's format, you'll have to read the manual, but here's a short walkthrough of the previous format:

  • The first part %07.7_Ax\n is the part that displays the last line that only contains the offset. Per the manual:

       _a[dox]     Display the input offset, cumulative across input files, of the
                   next byte to be displayed.  The appended characters d, o, and x
                   specify the display base as decimal, octal or hexadecimal
                   respectively.
    
       _A[dox]     Identical to the _a conversion string except that it is only
                   performed once, when all of the input data has been processed.
    
  • For the second: we now understand the "%07.7_ax " part. The 8/2 means 8 iterations and 2 bytes for the following, namely, "%04x ". Finally, after these, we have a newline: "\n".

I'm not really sure how you want your 21 bytes. Maybe this would do:

hexdump -e '"%07.7_Ax\n"' -e '"%07.7_ax " 21/1 "%02x " "\n"'

And you know how to get rid of the offset, if needed:

hexdump -e '21/1 "%02x " "\n"'
Clifton answered 1/11, 2014 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.