It should then be pretty simple to process this output with awk, and use printf("%c", $1) within awk to convert the interger value back to a binary character.
$ printf "abc\000\000\000de\000" >file1
$ dd if=file1 of=file2 cbs=3 conv=unblock
0+1 records in
0+1 records out
12 bytes (12 B) copied, 0 s, Infinity B/s
... you will now see newlines every 3 bytes...
$ od -hc file1; od -hc file2
0000000 6261 0063 0000 6564 0000
a b c \0 \0 \0 d e \0
0000011
0000000 6261 0a63 0000 0a00 6564 0a00
a b c \n \0 \0 \0 \n d e \0 \n
0000014
Use awk to remove null records and dd to convert back again...
$ awk '/[^\000]/' file2 > file3
$ dd if=file3 of=file4 cbs=3 conv=block
0+1 records in
0+1 records out
6 bytes (6 B) copied, 0 s, Infinity B/s
You should see that null records are removed...
$ od -hc file3; od -hc file4
0000000 6261 0a63 6564 0a00
a b c \n d e \0 \n
0000010
0000000 6261 6463 0065
a b c d e \0
0000006
That's just an example with a block size of 3, you would use cbs=31744
@Ygor, don't forget it's a binary file so there could be EOF CR or NULL characters dotted through the blocks of data. This will cause trouble for awk if the file is processed as-is.
Read in chunks with dd, test against a file of the same size full of binary zeroes, print if nonzero.
dd if=/dev/zero of=zero bs=31744 count=1
while dd count=1 bs=31744 > test
do
diff test zero > /dev/null || cat test
done < datain > dataout
rm -f test datafile
Assuming the file's content is an integral number of records, Corona688's solution will encounter the EOF during the first attempt to read in a new record. At that point, bpos is 0 and there's nothing in the buffer that wasn't written out during the previous iteration of the outer while loop.
If, however, the file contains a fractional record, that last fragment would indeed be discarded.
Your proposal would loop endlessly when EOF is encountered (read would return 0, bpos remains unchanged iteration after iteration, never breaking out of the inner while-read loop). Worse, in the face of repeated I/O errors, bpos would would be decremented by 1 during each iteration, until eventually it indexed a location beyond the buffer (triggering a segfault sooner or later ... or worse).
However, I will point out a minor copy-paste (I assume) mistake:
Regards,
Alister
---------- Post updated at 08:24 PM ---------- Previous update was at 07:30 PM ----------
Here's my contribution to this charming little problem:
If you know C you can fread each record of 31744 bytes into a buffer until eof...and compare each read byte string to null and print out only those that have non zero data.