Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to split a binary file into smaller files based on pattern 'xÚ' (it's 78 DA in hexadecimal), so when there's an 'xÚ' in the file the splitter script splits and pastes the content into a new file until another 'xÚ' can be found. The newly created files should start as 'xÚ'.

FreeBSD awk, grep, sh preferred.

share|improve this question


Ok here is the cleanup of that mess:

awk 'BEGIN { RS=""; FS=/\x78\xda/ } { for (i = 1; i <= NF; i++) print $i > i }' YOUR_BIN_FILE

Empty RS could still be causing unexpected results as it makes awk seperate records at blank lines. In fact it is difficult to circumvent a splitup of a file into records. One way to solve this is to set the record seperator to some pattern that is not present in the file. Another way would be to seperate records by 78da and process every record.

share|improve this answer
You could echo "78da$i" | xxd .... You don't need cat: xxd -ps -c $FILESIZE $MY_BINARY. You may need to set RS to null in awk to keep it from splitting on \x0a: BEGIN {RS=""}. You can make it easier to reassemble the files if you use printf to format the filenames: outfile=$(printf "outfile.%04d" $COUNT) (filenames will look like "outfile.0001"). You don't need redirection for output from xxd -r: xxd -r -ps -c $FILESIZE - $outfile. – Dennis Williamson Aug 10 '10 at 23:49
I like my cat ;) I needed the hint to empty RS, thanks! I removed the mess that caused eyes bleeding. – matthias krull Aug 11 '10 at 8:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .