Manupulating Records in a fixed width file

I am trying to determine what would be a fast and simple way to manipulate data that comes in a fixed width format. This data has 6 segments within a record. Each record needs to written out with a header and the 6 segments. Based on the value in column #6 the fields will be defined accordingly. The value in column #6 could be a number in the range of 1 -6.

The field that separates each record is in field 221. If column 221 has an "H", this is header field and all segments 1 - 6 need to be written together.

The header line has the following properties:
There is an "H" in column 221
column 1-6 = CIU
column 7-11 = DIS
column 12-16 = ZIP
column 21-70 = NAME

This header should be written at the top of each record. Under the header should be the data, which looks like this.

If column 221 has anything other than an "H", then it is the data that goes with the previous header. The next "H" in column 221 is the start of a new record.

If we determined that we have a new record that has an "H" in the 221 column the data following it should be assigned a label based on the value of column #6. Column #6 should always be an integer between 1 - 6.

If column 6 = 1
column 7-13 = TAP
column 20-21 = COL
column 29-30 = PMF
column 31-39 = NSS
column 40-47 = DOB
column 48-50 = AGE
...
...
column 208-216 = PAT

If column 6 = 2
column 14-21 = PR1
column 22-29 = PR2
column 30-37 = PR3
...
...
column 78-79 = NUM
column 80-84 = ALL
...
column 217-220 = REF_2

If column 6 = 3
column 14-16 = DRR
column 17-18 = CDM
column 24-31 = CASE
column 39-39 = MMM
...
column 77-81 = TIN

If column 6 = 4
column 204-213 = ID
column 214-215 = LOC

If column 6 = 5
column 128-145 = MCP_DI
column 146-146 = MCP_CD
column 147-165 = PRG_DI
column 166-195 = PRG_MN

If column 6 = 6
column 94-101 = 9PR
column 102-109 = 10PR
column 110-117 = 11PR
column 118-125 = 12PR
...
column 214-217 = HOLD

Here is an example record

Z18BTT0025 234684929347293TELESD DEF ..... over to field 221 = "H"
0025 1282190052150510282205296049821900521....over to field 221 = "E"
0025 2900521505127492001810787819191115..... over to field 221 = ""
0025 30660000000000000000000000000089723... over to field 221 = ""
0025 49090214430660000000000000000000000000... over to field 221 = "
0025 56051274942731 4280 27652 V090 ... over field 221 = ""
0025 60510282 over field 191 = DOE, JOHN... over field 221 = ""
F19AYY0025 2346789123456789ZTREWQ RRR ..... over to field 221 = "H"
0025 112749200181078781919111587Y87Y... over to field 221 = "G"
0025 211112222 D012345 D012345 D023456 R13123 over to field 221 = ""
0025 300000000000000000000000000051036842... over to field 221 = ""
0025 40660000000000000000000000000... over to field 221 = ""
0025 5060825Y25YFX4967443309A... over to field 221 = ""
0025 61234567... over to field 221 = ""
H20AYY0025 2346789123456789ZTREWQ EFD ..... over to field 221 = "H"

I realize that this a tough one to follow and any help would be appreciated. I have attempted doing this with awk and substr, but I am not getting any of logic to work. I think it may be best to do this with Perl, but my Perl programming is not that great. Now I am wondering am I going down the right path at all.

I believe it would be easier if you post a sample data with complete records (as an attachment) and an example of the desired output.

Hi.

And, while you are posting and pondering, please peruse perldoc -f pack and perldoc -f unpack.

Best wishes (with apologies for alliteration addiction) ... cheers, drl

I have attached a sample file with data. Hope this helps.

Sorry for the delay,
could you please also post an example of the desired output?