'making reference to file two by two in a bash command

I have this list of file that I have to analyse by pair (the a_1 with a_2, b_1 with b_2 and so on)

a_1.fq
a_2.fq
b_1.fq
b_2.fq
c_1.fq
...

I want to set a for loop to make reference to these pairs of file in a command, bellow this is just an example of what I want to do (with a false syntax) :

$ for File1 File2 in *1.fq *2.fq; do STAR --readFilein File1 File2 ; done

Thank you a lot for your help



Solution 1:[1]

You can iterate over just one type of the files and use parameter expansion to device the second one:

for file1 in *1.fq; do
    file2=${file1%1.fq}2.fq
    ...

The %pattern removes the pattern from the end of the variable's value.

You might want to check for the file2's existence before running the command.

Or, if you can get the files listed in the way the pairs are adjacent, you can populate $@ with them and shift the parameters by two:

set -- [a-z]_[12].fq
while (( $# )) ; do
    file1=$1
    file2=$2
    shift 2
    ...
done

Solution 2:[2]

You can try something like that:

for letter in {a..z}
do
 # Your logic
  echo "Working on $letter"
  cat $letter\_1.fq
  cat $letter\_2.fq
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Juranir Santos