catting together different files in unix
<p>I have a folder which has a bunch of files in them, and I'd like to cat together specific files. The layout is like this:</p>

<pre><code>sample1_S99_L1_R1_001.fastq.gz
sample1_S99_L2_R1_001.fastq.gz

sample1_S99_L1_R2_001.fastq.gz
sample1_S99_L2_R2_001.fastq.gz

sample2_S88_L1_R1_001.fastq.gz
sample2_S88_L2_R1_001.fastq.gz

sample2_S88_L1_R2_001.fastq.gz
sample2_S88_L2_R2_001.fastq.gz
</code></pre>

<p>and so on for different samples.
I would like to cat sample1_S99_*_R1_001.fastq.gz into 1 gunzip file. So the samples that are grouped in the code above should be catted together. </p>

<p>Manually I would do something like this:</p>

<pre><code>cat sample1_S99_L1_R1_001.fastq.gz sample1_S99_L2_R1_001.fastq.gz &gt; sample1.R1.fastq.gz
</code></pre>

<p>and </p>

<pre><code>cat sample1_S99_L1_R2_001.fastq.gz sample1_S99_L2_R2_001.fastq.gz &gt; sample1.R2.fastq.gz
</code></pre>

<p>My idea was to sort of automate this procedure and I came up with this:</p>

<pre><code>ls | awk -F '[_.]' '{print $1}' &gt; test.txt
sort -u test.txt &gt; names_final.txt
while read line;do cat $line_*_*_R1_*.fastq.gz &gt; $line.R1.fastq.gz;done &lt; names_final.txt
while read line;do cat $line_*_*_R2_*.fastq.gz &gt; $line.R2.fastq.gz;done &lt; names_final.txt
</code></pre>

<p>The problem I'm facing now is that these commands seems to cat all R1 files together. If i type for example</p>

<pre><code>ls sample1_*_*_R1_*.fastq.gz 
</code></pre>

<p>I only get the files I want to cat together. So I'm not sure were my mistake is here.</p>

<p>Anybody has an idea?</p>

<p>Thanks in advance.</p>