Your script deals with 4 files and you have only posted the contents of two of them, so I can't say what is wrong. However, from the output you are getting I would say that the required output is correct but the position isn't. A couple of things that may be causing your problem.
There are C/R...