This is a test version of Biostars. For the public version, visit https://www.biostars.org.
For loop with 2 variables

I have a large number of files. It is paired end sequencing. My files are of type _1.fastq.gz and _2.fastq.gz.

In the fastp command I must enter the 2 files corresponding to the same samples in _1 and _2 in the form fastp -i file_a_1.fastq.gz -I file_a_2.fastq.gz.

How could I do this for all my files without having to manually write the command with each file? When there is only one variable I do a loop for i in $ (ls Fastq_dir / *. fastq.gz) do ...

But with 2 variables?

bash fastp

Using parallel, try: parallel --dry-run fastp -i {} {=s/_1/_2/=} ::: *_1.fastq.gz or in bash loop try one of do echo $i ${i/_1/_2} or do echo $i ${i%_*}_2.fastq.gz.

1 answer

Assume all your data follow the same format:

for i in `ls *_1.fastq.gz`;
do
    second=`echo ${i} | sed 's/_1/_2/g'`
    # Do what ever with first fastq be ${i} and second be ${second}
done

Thanks a lot!

I have only one issue with that : my files is like this :

Sample_A_1_1.fastq.gz
Sample_A_1_2.fastq.gz
Sample_A_2_1.fastq.gz
Sample_A_2_2.fastq.gz
..

So it could replace Sample_A_1_1.fastq.gz by Sample_A_2_2.fastq.gz?

Instead of sed 's/_1/_2/g' maybe it could work with sed 's/_1.fastq/_2.fastq/g'

Use bash parameter expansion.

do_whatever ${i} ${i/_1.fastq.gz/_2.fastq.gz}

Or if you want to stick to sed,

second=$(echo $i | sed 's/_1.fastq.gz/_2.fastq.gz/')

Log in to answer this question.