title: Running zcat on multiple files using a for loop
body: <p>I'm very new to terminal/bash, and perhaps this has been asked before but I wasn't able to find what I'm looking for perhaps because I'm not sure exactly what to search for to answer my question.</p>
  
  <p>I'm trying to format some files for genetic analysis and while I could write out the following command for every sample file, I know there is a better way:</p>
  
  <pre><code>zcat myfile.fastq.gz | awk 'NR % 8 == 5 || NR % 8 == 6 || NR % 8 == 7 || NR % 8 == 0 {print $0}' | gzip &gt; myfile.2.fastq.gz
  zcat myfile.fastq.gz | awk 'NR % 8 == 1 || NR % 8 == 2 || NR % 8 == 3 || NR % 8 == 4 {print $0}' | gzip &gt; myfile.1.fastq.gz
  </code></pre>
  
  <p>I have the following files:</p>
  
  <pre><code>-bash-3.2$ ls
  BB001.fastq BB013.fastq.gz  IN014.fastq.gz  RV006.fastq.gz  SL083.fastq.gz
  BB001.fastq.gz  BB014.fastq.gz  INA01.fastq.gz  RV007.fastq.gz  SL192.fastq.gz
  BB003.fastq.gz  BB015.fastq.gz  INA02.fastq.gz  RV008.fastq.gz  SL218.fastq.gz
  BB004.fastq.gz  IN001.fastq.gz  INA03.fastq.gz  RV009.fastq.gz  SL276.fastq.gz
  BB006.fastq.gz  IN002.fastq.gz  INA04.fastq.gz  RV010.fastq.gz  SL277.fastq.gz
  BB008.fastq.gz  IN007.fastq.gz  INA05.fastq.gz  RV011.fastq.gz  SL326.fastq.gz
  BB009.fastq.gz  IN010.fastq.gz  INA1M.fastq.gz  RV012.fastq.gz  SL392.fastq.gz
  BB010.fastq.gz  IN011.fastq.gz  RV003.fastq.gz  SL075.fastq.gz  SL393.fastq.gz
  BB011.fastq.gz  IN012.fastq.gz  RV004.fastq.gz  SL080.fastq.gz  SL395.fastq.gz
  BB012.fastq.gz  IN013.fastq.gz  RV005.fastq.gz  SL081.fastq.gz
  </code></pre>
  
  <p>and I would like to apply the two zcat functions to each file, creating two new files from each one without writing it out 50 times. I've used for loops in R quite a bit but don't know where to start in bash. I can say in words what I want and hopefully someone can give me a hand coding it!: </p>
  
  <pre><code>for FILENAME.fastq.gz in all files in cd
  
  zcat FILENAME.fastq.gz | awk 'NR % 8 == 5 || NR % 8 == 6 || NR % 8 == 7 || NR % 8 == 0 {print $0}' | gzip &gt; FILENAME.2.fastq.gz
  zcat FILENAME.fastq.gz | awk 'NR % 8 == 1 || NR % 8 == 2 || NR % 8 == 3 || NR % 8 == 4 {print $0}' | gzip &gt; FILENAME.1.fastq.gz
  </code></pre>
  
  <p>Thanks a ton in advance for your help!</p>
  
  <p>*****EDIT*****</p>
  
  <p>My notation was a bit off, here's the final, correct for loop:</p>
  
  <pre><code>for fname in *.fastq.gz
  do
      gzcat "$fname" | awk 'NR % 8 == 5 || NR % 8 == 6 || NR % 8 == 7 || NR % 8 == 0 {print $0}' | gzip &gt;../../SeparateReads/"${fname%.fastq.gz}.2.fastq.gz"
      gzcat "$fname" | awk 'NR % 8 == 1 || NR % 8 == 2 || NR % 8 == 3 || NR % 8 == 4 {print $0}' | gzip &gt;../../SeparateReads/"${fname%.fastq.gz}.1.fastq.gz"
  done
  </code></pre>
  
  <p>*****FOLLOWUP QUESTION*****</p>
  
  <p>When I run the following:</p>
  
  <pre><code>for fname in *.1.fastq.gz
  do
  cat ./CleanedSeparate/XhoI/"$fname" ./CleanedSeparate/MseI/"${fname%.1.fastq.gz}.2.fastq.gz" &gt; ./FinalCleaned/"${fname%.1.fastq.gz}.fastq.gz"
  done
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>cat: ./CleanedSeparate/XhoI/*.1.fastq.gz: No such file or directory
  cat: ./CleanedSeparate/MseI/*.2.fastq.gz: No such file or directory
  </code></pre>
  
  <p>Obviously I'm not using * correctly. Any tips on where I'm going wrong?</p>