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?

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Ilya Smagin
  • 5,992
  • 11
  • 40
  • 57

1 Answers1

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"'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104