I have a binary file where records are separated by newlines. hexdump just dumps everything with a fixed column width. Is there a tool to hexdump this file, while honoring the newline separator? (Any other separator, like 0, would also be fine.)
1 Answer
This Python script will do it, sort of:
#!/usr/bin/env python3
import fileinput
for line in fileinput.input(mode='rb'):
print(line)
There's one problem that I hadn't realized: the binary data may itself contain the separator character, messing up the output.
-
1In what sense is that a hexdump? Maybe you meant
print(line.encode('hex'))or something...PM 2Ring– PM 2Ring2014-10-23 12:04:44 +00:00Commented Oct 23, 2014 at 12:04 -
It's a hexdump because the input is read in binary mode (
'rb').Frank Kusters– Frank Kusters2014-10-23 12:11:27 +00:00Commented Oct 23, 2014 at 12:11 -
-
You are right.
printwill try to interpret as UTF-8. A lot of characters in my data then print like this\xc8\x02\x00, which at first glance looks like a hexdump. However, it's Python's way of saying that these are non-printable characters.Frank Kusters– Frank Kusters2014-10-23 12:25:32 +00:00Commented Oct 23, 2014 at 12:25
\n=\x0a. Does the actual record data contain all other possible byte values, or is it restricted somehow? I suppose you'd like the hexdump output to be line by line, matching the binary data. Do you just want to see the hex data, or would you like output similar to the default output of either the hexdump or the hd programs?