Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 460
Oliver R. Avatar asked Feb 11 '14 21:02

Oliver R.


2 Answers

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

like image 160
John Kugelman Avatar answered Sep 30 '22 21:09

John Kugelman


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.

like image 23
Walter Tross Avatar answered Sep 30 '22 22:09

Walter Tross