text: user4401: My directories are organized as follows: one main directory, in which I have multiple directories that end with a number ranging from 314 to 727, followed by .3   . For example, 'mgm4761314.3'. Within these directories, I have fasta files named after a similar pattern (for example, mgm4761314.3.299.screen.passed.fna is in the directory mgm4761314.3).
  There is only one such file (.fna) per directory.
  What I've been trying to do is to write a loop that would go through all these folders and fasta files, and add the short number associated with the folder/file name (eg. 314, 315...727) to the headers. For example, for my first folder/fasta file, instead of:
  >seq1
  >seq2
  
  I want:
  >314_seq1
  >314_seq2
  
  The output would be exported to a file called, for instance, 314.fna.
  I've sort of made it work for one number at a time, but I have plenty of files, so I'd appreciate if anyone could help me with the loop part! This is what I tried (and it didn't work). Using ubuntu.
  for i in {314..727}; do sed "s/>/>${i}_/" [...]/*${i}.3/mgm4761${i}.3.299.screen.passed.fna > [...]/*${i}.3/${i}.fna; done
  
  
  user298: You were so close! This should do the trick:
  for i in {314..727}; do 
      sed -i.bak "s/^>/>${i}_/" mgm4761${i}.3/*.fna
  done
  
  Note that I am using -i which makes sed edit the original file. Because I use -i.bak, this will create a backup file with the original data. Once you are sure the command does what you need, you can delete all of these files with rm */*fna.bak.
  
original_text: 4401: <p>My directories are organized as follows: one main directory, in which I have multiple directories that end with a number ranging from 314 to 727, followed by .3   . For example, 'mgm4761314.3'. Within these directories, I have fasta files named after a similar pattern (for example, mgm4761314.3.299.screen.passed.fna is in the directory mgm4761314.3).</p>
  
  <p>There is only one such file (.fna) per directory.</p>
  
  <p>What I've been trying to do is to write a loop that would go through all these folders and fasta files, and add the short number associated with the folder/file name (eg. 314, 315...727) to the headers. For example, for my first folder/fasta file, instead of:</p>
  
  <pre><code>&gt;seq1
  &gt;seq2
  </code></pre>
  
  <p>I want:</p>
  
  <pre><code>&gt;314_seq1
  &gt;314_seq2
  </code></pre>
  
  <p>The output would be exported to a file called, for instance, 314.fna.</p>
  
  <p>I've sort of made it work for one number at a time, but I have plenty of files, so I'd appreciate if anyone could help me with the loop part! This is what I tried (and it didn't work). Using ubuntu.</p>
  
  <pre><code>for i in {314..727}; do sed "s/&gt;/&gt;<span class="math-container">${i}_/" [...]/*$</span>{i}.3/mgm4761<span class="math-container">${i}.3.299.screen.passed.fna &gt; [...]/*$</span>{i}.3/${i}.fna; done
  </code></pre>
   298: <p>You were so close! This should do the trick:</p>
  
  <pre><code>for i in {314..727}; do 
      sed -i.bak "s/^&gt;/&gt;<span class="math-container">${i}_/" mgm4761$</span>{i}.3/*.fna
  done
  </code></pre>
  
  <p>Note that I am using <code>-i</code> which makes <code>sed</code> edit the original file. Because I use <code>-i.bak</code>, this will create a backup file with the original data. Once you are sure the command does what you need, you can delete all of these files with <code>rm */*fna.bak</code>.</p>
  