open the output file enter a loop reading line by line print only the matching lines close and reopen send it again to the file and you have it.