tags:

views:

190

answers:

5

Hi 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 info 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 to verbose. I just want the contents.

A: 
$ strings file

seems like what you're looking for.

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

Flávio Amieiro
does not convert binary integer encodings to integers
tucuxi
Sorry. Not what he wants. Read the snippet.
zneak
+6  A: 

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.

Norman Ramsey
+2  A: 

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

Martin Beckett
So combining with Norman's and Nikolai's answers: _od -v -An -td4_ should give the wanted result
stefaanv
+3  A: 

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.

Nikolai N Fetissov
A: 

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.

drewk