Bash script to count and insert

Hi not sure if this is possible but I need some help with a bash script, I have a text file and on the first line that starts with 7150230 I need it to put a 1 at position 79 and a 2 at position 88, this is where it gets complicated, on the next line it finds that starts with 7150230 I then need it to put a 3 at position 79 and a 4 at position 88, next line that starts with 7150230 a 5 at position 79 and a 6 at position 88 and so on.

Below is an example of what I need it to do

71502304541-000            B1                    000000000000300100000001800001        2                                    S   

71502304540-000            B2                    000000000000300100000000000003        4

71502304541-000            B1                    000000000000300100000001800005        6                                    S   

71502304540-000            B2                    000000000000300100000000000007        8


Your input data is like this?

71502304541-000            B1                    00000000000030010000000180000                                              S
some data
71502304540-000            B2                    00000000000030010000000000000
some more data
here are more
71502304541-000            B1                    00000000000030010000000180000                                              S
71502304540-000            B2                    00000000000030010000000000000

Then you could do

awk '/7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)};'
71502304541-000            B1                    000000000000300100000001800001       2                                    S
71502304540-000            B2                    000000000000300100000000000003       4
71502304541-000            B1                    000000000000300100000001800005       6                                    S
71502304540-000            B2                    000000000000300100000000000007       8

Hi Jotne,

There are other lines of data so this way won't work, unless there's a way to keep those lines in?

Thank you for the help, it is much appreciated

You may retain remaining lines using

awk '/^7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, 7) "" ++a substr($0, 89)} !/^7150230/ {print}' inputfile

@firefox2k2

Post a much larger file (complete if its possible)

Hi that's kind of working problem is when it gets into double digits it's moving everything after up a place, like its inserting an extra character rather than copying over. I have highlighted the problem below, thanks again for all the help.

71402994728-100            Y700/0                0060000000540000ST0000051300000ST    001                S        G             
71502304541-000            B1                    000000000000300100000001800001        2                                    S   
71502304540-000            B2                    000000000000300100000000000003        4                                        
7130200619798130823CV10003    5500038441        024                  024                                                        
71402994729-100            Y701/0                0060000000540000ST0000052920000ST    001                S        G            
71502304541-000            B1                    000000000000300100000001800005        6                                    S   
71502304540-000            B2                    000000000000300100000000000007        8                                        
7130200619799130823CV10003    5500038442        024                  024                                                        
71402994730-100            Y702/0                0060000000600000ST0000052800000ST    001                S        G             
71502304541-000            B1                    000000000000500100000001200009        10                                    S   
71502304540-000            B2                    0000000000005001000000000000011        12 

Test if a>=10 , use different value.
@krishmaths no need for print at the end, its default action.

awk '/7150230/ {print substr($0, 1, 78) "" ++a substr($0, 80, a>=10?6:7) "" ++a substr($0, a>=10?89:88)} !/^7150230/' file

@Jotne - yes, default action is print and we don't need that. However I added it for better readability for a beginner.

Nearly there I think, slight problem, instead of count up its going 1 - 21, 3 - 41, 5 - 61

Also the second set of numbers 21,41,61 are at position 87 instead of 88, see below, thanks again for the help guys

71402144002-300            Y726B/0               0060000000800000ST0000028000000ST    001                S        G             
71502304413-000            B4                    000000000000100100000000000001       21                                    M   
715023044EC-000            B3                    000000000000800100000001000003       41                                    S   
715023044H0-000            B5                    000000000000100100000000000005       61                                        
7130200619802130823CV10003    5500048091        024                  024                                                        
71402144003-300            Y727B/0               0060000001200000ST0000028300000ST    001                S        G             
71502304413-000            B4                    000000000000100100000000000007       81                                    M   
715023044EC-000            B3                    000000000001200100000001000009       10                                    S   
715023044H0-000            B5                    0000000000001001000000000000011      12                                        
7130200619803130823CV10003    5500048085        024                  024                                                        
71402144005-300            Y729B/0               0060000000700000ST0000028700000ST    001                S        G             
71502304413-000            B4                    0000000000001001000000000000013      14                                    M   
715023044EC-000            B3                    0000000000004001000000017500015      16                                    S   
715023044H0-000            B5                    0000000000001001000000000000017      18                                        
7130200619804130823CV10003    5500107982        024                  024                                                        
71402938266-100            Y762/0                0060000000640000ST0000038080000ST    001                S        G             
71502304413-000            B4                    0000000000001001000000000000019      20                                    M   
715023044EC-000            B3                    0000000000008001000000008000021      22                                    S   
715023044H0-000            B5                    0000000000001001000000000000023      24