in reply to
Modify a txt file
You tell me what the real application for such a transformation is and show me the code you have tried and I'll help you get it right. I'm not about to write what looks like a homework answer for you however. And if it's not homework then you've probably simplified the problem to the point of meaninglessness.
Update: Oh, I see this probably relates to something you have been working on for a few days and most likely isn't Perl homework. Maybe it's time you filled us in on the bigger picture so we can help with a cohesive overall solution rather than drip feeding little parts of a solution as you run into trouble?
True laziness is hard work