title: How to create an output file (re)writed?
body: <p>I've been trying to solve my script but I really would appreciate some help.
  I have 2 input files. </p>
  
  <p>The first one is a multi-fasta file with headers like this:</p>
  
  <pre><code>'&gt;AH008024.2 Angelica acutiloba internal transcribed spacers 1 and 2, partial sequence'
  '&gt;AJ969149.1 Carthamus tinctorius partial ITS2'
  ....
  </code></pre>
  
  <p>(the quotes are just to let the > sign visible, otherwise it is not showed...)</p>
  
  <p>The second one is a primer validation file which looks like this:</p>
  
  <pre><code>AB280738.1,UniplantR,49,68,forward,CCCGHYTGAYYTGRGGTCDC,20,71.4,,,56.5 - 69.8
  AB280739.1,UniplantR,49,68,forward,CCCGHYTGAYYTGRGGTCDC,20,71.4,,,56.5 - 69.8
  AB280740.1,UniplantR,49,68,forward,CCCGHYTGAYYTGRGGTCDC,20,71.4,,,56.5 - 69.8
  ...
  </code></pre>
  
  <p>I want to write a "rewrited" version of the second file, changing the accession number "AB280738.1" for the species names from the fasta file, generating a tab delimited output like this:</p>
  
  <pre><code>AB280738.1      Glycyrrhiza uralensis ITS1, 5.8S rRNA and ITS2     UniplantR 49 68 forward CCCGHYTGAYYTGRGGTCDC 20 71.4   56.5 - 69.8
  
  AB280739.1      Glycyrrhiza glabra ITS1, 5.8S rRNA and ITS2      UniplantR 49 68 forward CCCGHYTGAYYTGRGGTCDC 20 71.4   56.5 - 69.8     
  ...
  </code></pre>
  
  <p>The final output must have the same number of lines that the second input file, the primers file, which in this case is 420 lines, but my current output is writing 292140 lines, it is doing the matching, but it is not writing properly.</p>
  
  <p>I've been working on this code that I'm showing to you.
  I see that the "matching" part of my script is working, but I think that I am not doing the right "push" step. Besides this, something is making my nested loops recursive, because there's multiple lines from the same match.
  To know that it is working, the output must have the same number of lines that the second input, the primers input.
  The second "print" tells me hou many times the pattern matched, and it is giving me 540 instead of 420.</p>
  
  <p>I would like to upload my input files and my current output if someone would like to try with it, but I couldn't find where upload files..</p>
  
  <pre><code>   #!/usr/bin/perl
     use diagnostics;
     use warnings;
     use strict;
  
     print "multifasta:\t";
     my $arq1 = &lt;STDIN&gt;;
     open (MYFILE, $arq1);
     my @file = &lt;MYFILE&gt;;
     close (MYFILE);
     print "file to rename:\t";
     my $arq2 = &lt;STDIN&gt;;
     open (MYFILE2, $arq2);
     my @file2 = &lt;MYFILE2&gt;;
     close (MYFILE2);
     my @new_file=();
     my $count = ();
     open (NEW_FILE, '&gt;&gt;plant_names_primer_bind_renamed.txt');
     foreach my $line2 (@file2) { 
             my @fields = split (/,/, $line2);
             my $accession2 = shift(@fields);
                     foreach my $line (@file) {
                             if ($line =~ /^&gt;/) {    
                             my $rev = reverse $line;
                             chop ($rev);
                             my $header = reverse $rev;
                             my @header = split (/ /, $header);
                             my $accession = shift (@header);
                                   if ($accession =~ /$accession2/)        {       
                     $count++;
                     print "$accession2 match $accession\t@header\t@fields\n\n";
                     print "$count\n";
                     push (@new_file, ("$accession2\t@header\t@fields"));
                     print NEW_FILE @new_file;
             }      
          }       
        }       
     }  
  
  
  
  
  
  </code></pre>