From the data you have shown, I have no idea what you are trying to do.
And, please use CODE tags when showing sample input, output, and code segments.
What operating system and shell are you using?
Is the idea that you take the second record from each set of input records with the same hour and minute, throw away the other records for that hour and minute, and replace everything from the 3rd colon through the 1st octothorp on the line you keep with Newnamen with increasing values of n for each hour and minute in your log?