1

I wish to use a for loop for a command that requires two input files. There is a single character change between the two files. Here are two example files:

Fay2_TCCGGAGA-CCTATCCT_L001_R1_001.fastq
Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq

Here is my attempt of the command:

for f in /directory/*R1*.fastq
 pref=${basename "$f" _*R1*.fastq}
command input1 $f input2 ${pref}_*R2*.fastq

The issue most likely lies in manipulating the basename so the loop uses the permutation of file1 to find file2. How do I make the basename shorter than the entire file name. I am getting the error

Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq_*R2.fastq does not exist

Thanks to comments below the looping is working properly but my files are being overwritten whenever a the command loops over a new file. The following is my entire command which requires naming both output files.

for f1 in /directory/*R1*.fastq
f2="${f1/_R1_/_R2_}"
 pref=${basename "$f" _*R1*.fastq}
command output1 ${pref}_trim_R1.fastq output2 ${pref}_trim_R2.fastq input1 $f input2 $f2

1 Answer 1

1

You can loop through the R1 files and use brace expansion to derive R2 file names:

for r1_file in /directory/*_R1_*.fastq; do
  r2_file="${r1_file/_R1_/_R2_}"
  command input1 "$r1_file" input2 "$r2_file"
done
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works great. I am having issues with the naming of these files now. I will edit my post to address this
Solved: Just change $f in the pref line to $f1. Thank you for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.