Output file with one byte per line in hex format (under linux bash)
Asked Answered
P

2

8

As the title says, here is an example:

$ cat test.txt 
ABCD
$ hd test.txt 
00000000  41 42 43 44 0a                                    |ABCD.|
00000005

my desired output would be:

41
42
43
44

I know that this is possible with sed, awk and stuff, but this might be very slow for large files. I thought of an format string for "hexdump" or a parameter combination for "od". Could you please help me out?

Pry answered 11/2, 2014 at 21:46 Comment(2)
what do you want to do with newlines?Preston
Oh, the newline just appeared in this example, the real file I want work with does not have newlines.Pry
P
7
od -An -vtx1 -w1 test.txt | cut -c2-

If you don't want newlines:

od -An -vtx1 -w1 test.txt | cut -c2- | fgrep -v 0a

Meaning of od options:

  • -An : addresses: no
  • -v : verbose, i.e., don't write a * instead of repeating lines
  • -tx1 : output type: hex, 1 byte (use -tx1z for an hd-like output)
  • -w1 : max width: 1

One nice thing about od is that it's available on any *x system.

Preston answered 11/2, 2014 at 21:51 Comment(3)
Thank you very much. That was exactly what i needed. Now that I have your examples I understand the manpage of od. Great job!Pry
Unfortunately, no od -w on macOSCaress
on macOS there is a gsed (brew install gnu-sed), and a few more, but unfortunately no god ;-)Preston
T
14
$ echo -n 'ABCD' | hexdump -v -e '/1 "%02x\n"'
41
42
43
44
0a

If you don't want it to print 0a at the end for the newline then I'd recommend getting rid of the newline from the file first.

Teratoid answered 11/2, 2014 at 21:53 Comment(2)
I prefer this one because I can add '0x' as a prefix or 'h' as a postfix.Continence
Works perfect even with 0dand 0a.Papen
P
7
od -An -vtx1 -w1 test.txt | cut -c2-

If you don't want newlines:

od -An -vtx1 -w1 test.txt | cut -c2- | fgrep -v 0a

Meaning of od options:

  • -An : addresses: no
  • -v : verbose, i.e., don't write a * instead of repeating lines
  • -tx1 : output type: hex, 1 byte (use -tx1z for an hd-like output)
  • -w1 : max width: 1

One nice thing about od is that it's available on any *x system.

Preston answered 11/2, 2014 at 21:51 Comment(3)
Thank you very much. That was exactly what i needed. Now that I have your examples I understand the manpage of od. Great job!Pry
Unfortunately, no od -w on macOSCaress
on macOS there is a gsed (brew install gnu-sed), and a few more, but unfortunately no god ;-)Preston

© 2022 - 2024 — McMap. All rights reserved.