There are some things I don't understand in your code (such as what $newfile is used for), but perhaps that's because not all the code is shown. The main thing I see is that when you are looping over $out1 you are constantly overwriting the variables $myid, etc, with your split so you are going to get results concerning the last line only...and I don't see any attempt to search for a line of a given form (containing some "unique value".)