Thursday, September 22, 2011

Extract bytes from large file.

Sometimes you want to be able to copy data from the middle of a binary file.

In my case it was from MPEG2 Transport Streams and I wanted to copy out some data from deep within a massive multi gigabyte file.

extract is a simple bash script that calls DD and copies out bytes from the middle of the file.

#!/bin/bash
if [ 4 -ne $# ]; then
echo
echo "Usage: extract [infile] [outfile] [start] [len]"
echo
echo "extract will copy len bytes from start post from infile to outfile"
echo
exit
fi

IFILE=$1
OFILE=$2
FSTART=$3
FLEN=$4
dd bs=1 if=$IFILE of=$OFILE seek=0 skip=$FSTART count=$FLEN

No comments: