6

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
2
  • This should probably be merged with stackoverflow.com/questions/28725333/… but I'm hesitant to mark them as duplicates at this time. Commented May 5, 2017 at 7:59
  • @tripleee, I reopened this because it was closed with a duplicate that only referred to arrays and not files, but many of the solutions are file-specific (and similarly, in the array case, there are many array-specific approaches). Commented Dec 13, 2023 at 21:26

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@perreal...it worked man #!/bin/bash paste files_in.txt files_out.txt | while read if of; do echo "java -cp *:ext/*: es.blast2go.prog.B2GAnnotPipe -in $if -out results/$of -prop b2gPipe.properties -v -annot -dat" done
1

You can use two different read commands inside your while loop's condition, reading from two different file descriptors.

while read -r i <&3 && read -r j <&4; do
  java es.blast2go.prog.B2GAnnotPipe \
    -in "$i" \
    -out "results/$j" \
    -prop b2gPipe.properties -v -annot -dat
done 3<file_in.txt 4<file_out.txt

Putting the 3<file_in.txt after the done makes file descriptor 3 read from file_in.txt for the duration of the loop. Similarly, putting 4<file_out.txt makes file descriptor 4 read from file_out.txt. Consequently, <&3 and <&4 can be attached to any command within the loop to cause stdin to be attached to the chosen file for the duration of that command.

Comments

0

As long as both your lists are just repetitions of the same kind of name with successive numbers, you shouldn't iterate over files at all. Instead, just count a variable and use that in whatever command you want executed at each step. Example:

COUNT=1
while [ $COUNT -lt 5 ]; do
  mv inputfile$COUNT outputfile$COUNT
  let COUNT++
done

Comments

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.