Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Bash script to loop through two files [duplicate]

Tags:

bash

loops

I have a file (file_in.txt) containing these names:

aphid_splitseq.1.fasta.annot.xml
aphid_splitseq.2.fasta.annot.xml
aphid_splitseq.3.fasta.annot.xml
aphid_splitseq.4.fasta.annot.xml
aphid_splitseq.5.fasta.annot.xml

and I have another file (file_out.txt) with these names:

aphid_splitseq_1
aphid_splitseq_2
aphid_splitseq_3
aphid_splitseq_4
aphid_splitseq_5

Now I want statements like this

java -cp *:ext/*: es.blast2go.prog.B2GAnnotPipe -in aphid_splitseq.1.fasta.annot.xml -out results/aphid_splitseq_1 -prop b2gPipe.properties -v -annot -dat 

Basically, I want to loop through each of file_in.txt and file_out.txt and replace the values of -in and -out with i and j respectively.

I have tried it in Bash, but it doesn't seem to work:

for i in `cat file_in.txt`
    for j in `cat file_out.txt`; do
        java -cp *:ext/*: es.blast2go.prog.B2GAnnotPipe -in $i -out results/$j -prop b2gPipe.properties -v -annot -dat
    done
done
like image 271
upendra Avatar asked Sep 20 '13 06:09

upendra


1 Answers

paste can be helpful:

#!/bin/bash

paste file_in.txt file_out.txt | while read if of; do
  echo "-in $if -out $of"
done

yields:

-in aphid_splitseq.1.fasta.annot.xml -out aphid_splitseq_1
-in aphid_splitseq.2.fasta.annot.xml -out aphid_splitseq_2
-in aphid_splitseq.3.fasta.annot.xml -out aphid_splitseq_3
-in aphid_splitseq.4.fasta.annot.xml -out aphid_splitseq_4
-in aphid_splitseq.5.fasta.annot.xml -out aphid_splitseq_5

you can modify this to get the desired behaviour.

like image 181
perreal Avatar answered Sep 24 '22 09:09

perreal