in reply to File manipulation

You're doing yourself a disservice by asking us how to implement this. What that says is that you don't know how to solve the problem. It's not a question of how to program this in Perl, it's a question of how to program.

If you break the task into parts, you can then see what needs to be done.

Ask the follow-up questions: when do you write out the line in the desired format? Do you need to do anything special when you've finished reading the input file?

You can write the program to do this task. You just need to think about it.