reformat one record from two records

I have not get much answer/solution for the posting. Here I break down the question and hope to get some help.

  1. How can I use AWK to read in two records at the same time and keep loop to next two when the condition is meet?

position 1-10 --> Unique to identity whether there is secondary record or not
--> If there is more than one record with the same value for this portion,
it means there is secondary record;
otherwise, there is only primary record
position 11 ---> 0 means the record is the secondary
1 means the record is the primary
Output file ----> starting from position 12
Segment definition --->starting from position 36 (TTTT)

           XXXX\#\#\#
        XXXX---> Segment ID 4 bytes , eg TTTT or SH01
        \#\#\# ---> Total length of segment 020 means segment is 20 bytes long
        See below string has two segment, 
        first one id is TTTT and length is 15 bytes long; 
        second one is is SH01 and length is 8 bytes long
        TTTT015cvsdbfffSH01008X

ENDS segment format --->ENDS010###
ENDS010 --> segment id and length
### represents the total number of segment in current records.
For example ENDS010004 means there is 4 segments in the record including ENDS010 segment
-----------------
rules

  1. if group by position 1-10 have one record, then reformat the string by cutting off first 11 bytes and output

  2. if group by position 1-10 have two record, then
    for the record with value as 1 in position 11,
    then reformat string by
    a. cutting of the first 11 bytes
    b. recount the number of segments
    c. append ENDS010### segment at the end of string

    for the record with values as 0 in position 11,
    then reformat string by
    a. cut the first two segments from primary records and append them at the beginning of the output string
    b. recount the number of segments
    c. append ENDS010### segment at the end of string
    ----------------
    Attached are two example files, one for input and one for output

What have you done so far?

I have not done any yet. I am still looking for how I can read in the second record while holding the first.

Thanks for your respond.

Check here Transpose Data from Columns to rows Post: 302590507 and here Construct 3 column table from one column list Post: 302544912

Thanks