Filter and write in to output file