Another minor suggestion AFTER you cleared up like rovf suggested: Code will be more transparent if you store the filenames in an array: @files = <file1>
Than you can make the foreach $file(@files) loop and grep, storing the results in a simple array, or file, as you wish.