How to print only the hex values from hexdump without the line numbers or the ASCII table? [duplicate]
Asked Answered
B

4

53

following Convert decimal to hexadecimal in UNIX shell script

I am trying to print only the hex values from hexdump, i.e. don't print the lines numbers and the ASCII table.

But the following command line doesn't print anything:

hexdump -n 50 -Cs 10 file.bin |  awk '{for(i=NF-17; i>2; --i) print $i}'
Begird answered 21/3, 2013 at 16:53 Comment(2)
age is not the primary factor, but rather upvotes and answer quality ;-) meta.#252438Woolley
hexdump file.bin | sed "s/[^ ]* //1" This will remove the line numbers, by default the ascii table wouldn't be printed.Marijn
A
68

You can specify the exact format that you want hexdump to use for output, but it's a bit tricky. Here's the default output, minus the file offsets:

hexdump -e '16/1 "%02x " "\n"' file.bin

(To me, it looks like this would produce an extra trailing space at the end of each line, but for some reason it doesn't.)

Ambassadress answered 21/3, 2013 at 17:54 Comment(3)
Great!! Usefull for me this answer! But I think is better also to have -C option. And if you add cut -c 9- | head -n 1 your output will display only the hex numbers.Britanybritches
This blog entry shows more examples how to modify the output format of hexdump.Eckman
Add -v to list every line and not to compress same lines with *Ronda
B
85

Using xxd might be a better option for this task:

xxd -p -l 50 -seek 10 file.bin

From man xxd:

xxd - make a hexdump or do the reverse.

    -p | -ps | -postscript | -plain
        output in postscript continuous hexdump style. Also known as plain hexdump style.

    -l len | -len len
        stop after writing <len> octets.
 
    -seek offset
        When used after -r: revert with <offset> added to file positions found in hexdump.
Begird answered 21/3, 2013 at 17:53 Comment(1)
@andsens hexdump is in busybox, and thus available on embedded, whilst xxd isn't :ЬEchinus
A
68

You can specify the exact format that you want hexdump to use for output, but it's a bit tricky. Here's the default output, minus the file offsets:

hexdump -e '16/1 "%02x " "\n"' file.bin

(To me, it looks like this would produce an extra trailing space at the end of each line, but for some reason it doesn't.)

Ambassadress answered 21/3, 2013 at 17:54 Comment(3)
Great!! Usefull for me this answer! But I think is better also to have -C option. And if you add cut -c 9- | head -n 1 your output will display only the hex numbers.Britanybritches
This blog entry shows more examples how to modify the output format of hexdump.Eckman
Add -v to list every line and not to compress same lines with *Ronda
P
30

As an alternative, consider using xxd -p file.bin.

Plossl answered 21/3, 2013 at 17:15 Comment(0)
L
3

First of all, remove -C which is emitting the ascii information.

Then you could drop the offset with

hexdump -n 50 -s 10 file.bin | cut -c 9-
Longley answered 21/3, 2013 at 17:6 Comment(1)
it seems to work but changes the endian...Begird

© 2022 - 2024 — McMap. All rights reserved.