Issue
I'm trying to copy a chunk from one binary file into a new file. I have the byte offset and length of the chunk I want to grab.
I have tried using the dd
utility, but this seems to read and discard the data up to the offset, rather than just seeking (I guess because dd
is for copying/converting blocks of data). This makes it quite slow (and slower the higher the offset). This is the command I tried:
dd if=inputfile ibs=1 skip=$offset count=$datalength of=outputfile
I guess I could write a small Perl/Python/whatever script to open the file, seek to the offset, then read and write the required amount of data in chunks.
Is there a utility that supports something like this?
Solution
Yes it's awkward to do this with dd today. We're considering adding skip_bytes and count_bytes params to dd in coreutils to help. The following should work though:
#!/bin/sh
bs=100000
infile=$1
skip=$2
length=$3
(
dd bs=1 skip=$skip count=0
dd bs=$bs count=$(($length / $bs))
dd bs=$(($length % $bs)) count=1
) < "$infile"
Answered By - pixelbeat