I need to concatenate four binary, meaning non-text files. Lets say I have four files:
file1:
AAAA
file2:
BBBB
file3:
CCCC
file4:
DDDD
Now I want my resulting file to look like: ABCDABCDABCDABCD. So I want to alternate every byte. I would also like to be able to alternate every n bytes meaning alternate every 1,2,3,4,etc bytes.
So far, most of the information I searched for deals with text files and uses cat & paste which appears to only work with lines of text. I also cant find any data on alternating concatenation for more than two files.
21 Answer
This should work. Set each to the number of bytes to read each time.
len=$(stat -c %s file1)
each=1
while [ $len -gt 0 ]
do dd bs=$each count=1 <&5 dd bs=$each count=1 <&6 dd bs=$each count=1 <&7 dd bs=$each count=1 <&8 let len=len-$each
done 5<file1 6<file2 7<file3 8<file4 2>/dev/null 0