开发者

Is there a nice Unix command for dumping the text representation of a binary file?

I got some binary files containing integers. Is there some nice Unix command, that will allow me to dump it to a terminal without offset information, etc.?

Something like:

double int[4];
while (fread(tmp, sizeof(int), 4, stdin))
    for(int i = 0; 开发者_运维技巧i < 4; i++)
        printf("%d\t",tmp[i]);

It seems that hexdump and od gives me the information I want, but the output is too verbose. I just want the contents.


To solve this kind of problem using standard Unix tools, you typically pipe a bunch together:

od -v -t d4 ~/.profile | awk '{$1 = ""; print}' | fmt -1 | sed 's/^ *//'

The od prints every 32-bit word in decimal, with preceding offsets. The awk comment removes the offsets. The fmt command forces one integer per line. The sed command strips leading spaces off the result.


Try the following hexdump(1) incantation:

hexdump -v -e '4/4 "%d\t"' file

-v forces printout of the whole file (no * for repeating lines),
-e takes format string '4/4 "%d\t"' that tells hexdump(1) to iterate four times, printing four bytes at each iteration with given printf-like mask.


Doesn't od -An filename give you the data without the offset?


You can replicate almost exactly your desired input and output with Perl. There is literally no file read or write that cannot be done, most more easily than in C.

The 'sysread' function is most similar to C's fread.

You can specify a file handle, automatic buffer, bytes to read, and offset into the file. And in Perl, the opening and closing of a file is automatic in most cases.

You did not specify enough info to write an example, but this link should get you started:

Cookbook Ch 8

Or just google 'perl sysread'

What you want can probably be done in a Perl 1 line program.


strings seems like what you're looking for:

$ strings file

It will output all the strings contained in the binary file. It is still a little verbose for a big file, but it is at least readable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜