Iterate over items in one array and groups of items in second array

62 views Asked by At

I have two arrays containing file names

files_ref=( $(find . -name '*.fasta') )
files_fq=( $(find . -name '*.fastq') )

I understand that if I want to iterate over pairs of items I would do something like this:

for i in "${!files_ref[@]}"; do
    printf "%s is in %s\n" "${files_ref[i]}" "${files_fq[i]}"
done

However, my second arrays looks like this

file_ref=(
  DI1.fasta
  DI2.fasta
  WT1.fasta

)
files_fq=(
  DI1_P1.fastq
  DI1_P2.fastq
  DI1_P3.fastq
  DI2_P1.fastq
  DI2_P2.fastq
  DI2_P3.fastq
  WT1_P1.fastq
  WT1_P2.fastq
  WT1_P3.fastq
)

I would have to iterate over files_ref with all its correspoding items in the same group:

DI1.fasta with DI1_P1.fastq DI1_P2.fastq DI1_P3.fastq
DI2.fasta with DI2_P1.fastq DI2_P2.fastq DI2_P3.fastq
WT1.fasta with WT1_P1.fastq WT1_P2.fastq WT1_P3.fastq

Any suggestions?

...to be more clear

I would need iterations like this:

DI1.fasta DI1_P1.fastq
DI1.fasta DI1_P2.fastq
DI1.fasta DI1_P3.fastq
DI2.fasta DI2_P1.fastq
DI2.fasta DI2_P2.fastq
DI2.fasta DI2_P3.fastq
WT1.fasta WT1_P1.fastq
WT1.fasta WT1_P2.fastq
WT1.fasta WT1_P3.fastq
1

There are 1 answers

1
Simone On BEST ANSWER

One way you could go about it is by extracting the prefixes first (DI1, DI2, WT1) and then doing a double iteration. For example this Bash script would produce the output you want:

prefixes=()
for name in "${file_ref[@]}"; do
    prefixes+=("${name%.*}")
done

for prefix in "${prefixes[@]}"; do

    echo -n "$prefix.fasta with "

    for fq in "${files_fq[@]}"; do
        if [[ $fq == $prefix* ]]; then
            echo -n "$fq "
        fi
    done
    echo # add a newline
done