0

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
  • I doubt a ready-made tool exists to do this, but it's probably fairly easy to cobble together a shell script to do it. Are all the files of the exact same size?
    – user
    Commented Aug 23, 2015 at 17:52
  • @MichaelKjörling Yes, The files are the same size.
    – Mister Tea
    Commented Aug 23, 2015 at 20:44

1 Answer 1

1

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

You must log in to answer this question.

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