You can use awk using a script like that (tst.awk):
BEGIN{print "#reference_group_id serials_from_ref_group group_id serials_from_group"}
$1 ~ /^reference_/ {ref=$1;ser=$2;next}
{
for(i=1;i<=length($2);i++){
print ref, substr(ser,i,1), $1, substr($2,i,1)
}
}
I supposed that your reference_group_id is always begining with reference_ to store it to a var called ref, and store the serials_from_ref_group to ser. We then use both these var in a loop.
Then a line like that should work :
awk -f tst.awk file
As your output is formatted by column you can pipe the output to column -t
awk -f tst.awk file | column -t
Explanation for awk script :
BEGINis executed once only, before the first input record$1 ~ /^reference_/if$1matches the regular expression^reference_length($2)length of the second fieldsubstr(ser,i,1)substring ofserstarting at theiposition and length of1