dd question

fred smith fredex at fcshome.stoneham.ma.us
Fri Dec 10 03:41:47 UTC 2010


On Fri, Dec 10, 2010 at 03:11:25AM +0000, Amadeus W.M. wrote:
> I have a binary file with data. Each block of 48 bytes is a record. I 
> want to extract the first 8 bytes within each record. I'm thinking this 
> should be possible with dd, but gawk, perl - anything goes. It just has 
> to be fast, because the data files are ~ 1Gb.
> 
> I can do this in C++ but I was just wondering if it can be done with 
> existing well tested tools.
> 
> Any suggestions? Thanks!

May be able to do something with cut. It may depend on if the records
are newline-terminated or not. but using cut you can specify a set of
bytes to be output (while the rest of the line is discarded), so you
may be able to do something like this:

dd if=yourfilename bs=48 | cut -b 1-8 > outfile

ah. just did a quick hack test with a file containing this:

a234567890123456789012345678901234567890
b234567890123456789012345678901234567890
c234567890123456789012345678901234567890
d234567890123456789012345678901234567890

and this seems to do the trick:

cut filename -b 1-8

giving:

a2345678
b2345678
c2345678
d2345678

which looks like what you wanted.

however this is a text file with newlines,... if it has no newlines
your mileage may vary. in that case you could try some sed or awk
hacks to insert newlines after each 48 bytes then feed it to cut.

good luck!

-- 
---- Fred Smith -- fredex at fcshome.stoneham.ma.us -----------------------------
                       I can do all things through Christ 
                              who strengthens me.
------------------------------ Philippians 4:13 -------------------------------


More information about the users mailing list