Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My directory structure is given below

x:\Project_2012-158A\Sample_4041

SampleSheet.csv
4041_CGTACG_L002_R1_001.fastq
4041_CGTACG_L002_R2_001.fastq
4041_CGTACG_L006_R2_001.fastq
4041_CGTACG_L006_R1_001.fastq

x:\Project_2012-158A\Sample_4027

SampleSheet.csv
4027_TAGCTT_L002_R2_001.fastq
4027_TAGCTT_L006_R1_001.fastq
4027_TAGCTT_L002_R1_001.fastq
4027_TAGCTT_L006_R2_001.fastq

x:\Project_2012-158A\Sample_D425

SampleSheet.csv
D425_ACTGAT_L008_R2_001.fastq
D425_ACTGAT_L008_R1_001.fastq
D425_ACTGAT_L004_R2_001.fastq
D425_ACTGAT_L004_R1_001.fastq

I want to concatenate the files with "R1" and "R2" separately for every sample. I know

cat file1.fastq file2.fastq > concatenation.fastq 

will give concatenate but how do I do that for all the sudirectories with single script?

share|improve this question
3  
Backslashes and drive letters ... are you on Windows? Unless you are asking about something like cygwin, you are probably on the wrong site. –  jw013 Nov 14 '12 at 18:09
    
No. This directory structure I created using windows. I want to perform my tasks on Linux. –  Jana Nov 14 '12 at 18:43
add comment

2 Answers 2

up vote 5 down vote accepted
cd /path/to/Project_2012-158A &&
for dir in Sample*/; do
    for r in R1 R2; do
        outfile=${dir%/}_${r}.fastq
        glob=*_${r}_*.fastq
        cat "$dir"/$glob > "$dir/$outfile" &&
        rm -f "$dir"/$glob
    done
done
share|improve this answer
    
Thanks @jw013. I'm new here. I wish I could vote you up. –  Jana Nov 14 '12 at 21:02
1  
Note that except with zsh, that would create empty files for Sample directories that don't have R1 or R2 files. –  Stéphane Chazelas Nov 14 '12 at 21:02
    
@StephaneChazelas You are right. If the OP wants, it'd be trivial to check for and remove the empty files. I also considered adding lines to remove the original files but eventually decided not to unless the OP specifically asks for it. For now, I just gave a simplest example. –  jw013 Nov 14 '12 at 21:05
    
@jw013 Is there anyways I could save the file names with SampleXXX_R1.fastq and SampleXXX_R2.fastq for every samples? and also delete the original files? –  Jana Nov 15 '12 at 22:13
    
Thanks @jw013. Will do it next time –  Jana Nov 16 '12 at 0:13
show 6 more comments

Like this:

cat ./*/*R2* > result

* - match everything

Be aware that this will get every occurrence of R2 in the filename.

share|improve this answer
    
Thanks @week, but this concatenate all files with R2 into one file. I don't want that. I wan't the files to be concatenated under "every" sub directory. ex: under 'Sample_4041' directory all files with R2 should be concatenated and the same applies to other sub directories as well. At the end I'll have only two files under each sample sub directory –  Jana Nov 14 '12 at 19:11
1  
@Jana add that to your question - it's an important part of the requirement. –  jw013 Nov 14 '12 at 19:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.