Joining ends of strings in certain order with repeated ID's

I posted this a few days ago and got some help (Putting together substrings if pattern is matched - Page 2 | Unix Linux Forums | Shell Programming and Scripting)

But I am now stuck on an issue that is similar but not the same really. I want to join parts of one line with parts of another line that share the same ID. The ID can repeat many times. Below I show an example of 4 repeats of the same ID. The combinations I want to generate are the following:

end of 4 with beginning of 4
            with beginning of 3
            with beginning of 2
end of 3 with beginning of 3
            with beginning of 2
end of 2 with beginning of 2
end of 1 with beginning of 1

The substrings that should be joined should be 9 characters in length. Here are the 4 repeated ID's as input:

%dog  aaaaaaaaaaAAAAAAAAA
%dog  bbbbbbbbbbBBBBBBBBB
%dog  cccccccccccCCCCCCCCC
%dog  xxxxxxxxxxXXXXXXXXX

the output should look like this:

%dog  XXXXXXXXXxxxxxxxxx
%dog  XXXXXXXXXccccccccc
%dog  XXXXXXXXXbbbbbbbbb
%dog  CCCCCCCCCccccccccc
%dog  CCCCCCCCCbbbbbbbbb
%dog  BBBBBBBBBbbbbbbbbb
%dog  AAAAAAAAAaaaaaaaa

How can this be done?

Does the file just a few records or hundreds/thousands or more records?

If it has hundreds/thousands or more records, Is this the correct rules for outputting the new file?

f1    f2        f3
%dog  aaaaaaaaaaAAAAAAAAA
%dog  bbbbbbbbbbBBBBBBBBB
%dog  cccccccccccCCCCCCCC
%dog  xxxxxxxxxxXXXXXXXXX

if f3 = XXXXXXXXX
 then f1, f2 from record where f2 = xxxxxxxxx or ccccccccc or bbbbbbbbb
 then f3

if f3 = CCCCCCCCC
 then f1, f2 from record where f2 = ccccccccc or bbbbbbbbb
 then f3

if f3 = BBBBBBBBB
 then f1, f2 from record where f2 = bbbbbbbbb
 then f3

if f3 = AAAAAAAAA
 then f1, f2 from record where f2 = aaaaaaaaaa
 then f3

hi spacebar, the rules you wrote out are correct. I should just mention that the file only has two columns (ID and string column). I made the last segments of the strings capital letters for clarity in the post but the strings are really all lowercase.

I should also mention that the ID can repeat more than 4 times, and that there exists multiple ID's, each with varying numbers of repetitions.

Does the ID always have "whitespace" following it?
How long is "string" field, always 19?
The last part of the string will always be 9 positions and the first part 10?

after the ID is a tab space. The string field varies in length, but the parts that should be joined should just be 9 character substrings from either the beginning or the end. In total, each output string should be 18 characters long

Can you post a sample of the input file and the output desired because from your post it aint clear what exactly you want to do...

Here's a sample input for an example of 3 occurrences. I want to generate specific combinations of a 9 character substring based on the last occurrence of the ID %dog (line 3) with the beginning substring of the second occurrence of the ID.

input:

%dog  CATCAToooooooooooooooDOGDOG
%dog  HIHIoooooooooooooooBYEBYE
%dog  hellooooooooooooooooGOGOGO

output:

%dog   oooGOGOGOHIHIooooo

In this example, I show what the output looks like for 1 of the rules. Notice how the last 9 character substring from the third line is now in the beginning of column 2 and it is followed by the first 9 character substring from the second occurrence of the line with the ID %dog. Another possibility exists and that is to swap the beginning and end substrings of the last line (line 3) with itself. And also to swap the last substring with the beginning substring of the first line with itself.

output:

%dog   oooGOGOGOhellooooo
%dog   oooDOGDOGCATCATooo

The final output for this example should have three lines, based on the following rules:

1) last substring of line 3 with beginning substring of line 3
2) last substring of line 3 with beginning substring of line 2
3) last substring of line 1 with beginning substring of line 1

final output:

%dog   oooGOGOGOhellooooo
%dog   oooGOGOGOHIHIooooo
%dog   oooDOGDOGCATCATooo

Does this help?

What if you have more than 3 or less than 3 occurrences...what then are the rules etc...

If there are for example 5 occurrences, then it should be like this:

1) last substring of line 5 with beginning substring of line 5
2) last substring of line 5 with beginning substring of line 4
3) last substring of line 4 with beginning substring of line 3
4) last substring of line 3 with beginning substring of line 2
5) last substring of line 1 with beginning substring of line 1