For loop with 2 variables
1
0
Entering edit mode
18 months ago
quentin54520 ▴ 90

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 ? for loop variable fastp • 1.6k views ADD COMMENT 0 Entering edit mode 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. ADD REPLY 1 Entering edit mode 18 months ago Sam ★ 4.5k 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

0
Entering edit mode

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 Etc. 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'

2
Entering edit mode

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/')