Concatenate four binary files using alternating pattern in Linux

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.

2

1 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like