1

I'm looking for a solution in bash to rename multiple fastq files in a bioinformatics pipeline in a for loop, or something similar. The information is contained in two lists.

List A

SRR11111111
SRR11111112
SRR11111113
SRR11111114
SRR11111115

List B

Sample1
Sample2
Sample3
Sample4
Sample5

I need to change the names of the fastq files from those contained in list A to the names in list B. So the fastq file named SRR11111111 (position 1 in list A) becomes sample1 (position 1 in list B) and so on. Although, the loop below is nonsense, it gives you an idea of what I want to do.

for file in listA
do

mv listA[position] listB[position]

done

In other packages such as python I would use a dictionary for this, using key-value pairs, or use a config file but not sure of the most efficient method to do this in bash.

This question is similar but not exactly what I'm looking for.

Darren
  • 277
  • 4
  • 17

3 Answers3

4

You can use paste in a process substitution:

while read -r old new; do
   echo mv "$old" "$new"
done < <(paste listA listB)

mv SRR11111111 Sample1
mv SRR11111112 Sample2
mv SRR11111113 Sample3
mv SRR11111114 Sample4
mv SRR11111115 Sample5 

When you're satisfied with output, remove echo before mv.

anubhava
  • 761,203
  • 64
  • 569
  • 643
2

You could use following awk too here.

awk 'FNR==NR{a[FNR]=$0;next} {system("echo mv " a[FNR] OFS $0)}' ListA ListB

Remove echo in above code once you are satisfied with commands printed over screen.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2
while IFS= read -r fileA && IFS= read -ru 3 fileB; do
    mv -- "$fileA" "$fileB"
done < listA 3< listB
PesaThe
  • 7,259
  • 1
  • 19
  • 43