Loop to concatenate multiple pairs of files with a

2019-08-12 21:56发布

问题:

I have a very basic question, but I can't get a solution. I have multiple files in the same directory and I would like to concatenate each pair of files. The names are:

Sample1_R1_L001.fastq Sample1_R2_L001.fastq Sample2_R1_L001.fastq Sample2_R2_L001.fastq Sample3_R1_L001.fastq Sample3_R2_L001.fastq

(etc...)

The result I want is to concatenate by sample, such as cat Sample1_R1_L001.fastq Sample1_R2_L001.fastq > Sample1_concat.fastq

I tried this loop, find . -name "_R?_"|while read file; do "$file"R1*.fastq "$file"_R2_L001.fastq > "$file"_merged.fastq

but it didn't work. Any thoughts?

回答1:

Here's how I'd do it:

for i in {1..4}; do echo cat Sample"$i"* > combined_"$i".txt; done

Loop over the numbers then build the command... getting the actual filenames is surely possible, but with shell, I just do the simplest thing that can possibly work, even if that means partially brute forcing the loop.

Replace the 1..4 to whatever the limits are and the shell will expand that into all the various numbers for you.



标签: loops cat fastq