0

Let's say I have a txt file like:

CGND_01234    CGND-HRA-00736
CGND_34563    CGND-HRA-00567
...

How do I create a for loop for the below line in bash that iterates every line in the file?

find ./${first_word_in_the_first_line} -name "${second_word_in_the_first_line}*.bam" -type f 
find ./${first_word_in_the_second_line} -name "${second_word_in_the_second_line}*.bam" -type f
...
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

6

A while read loop is a common way to read a file line-by-line. Conveniently, if you pass multiple variable names to read it will also split the line at the same time.

while read -r dir file; do
    find "$dir" -name "$file*.bam" -type f 
done < file.txt
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • This should probably use `read -r` (re. `shellcheck`) and a different FD (`read -u9` & `done 9< file.txt`, for example) in case OP wants to use a stdin-swallowing command inside the loop. – l0b0 Mar 18 '21 at 02:43
  • I added `read -r`. I'll pass on the different FD idea; don't want to overcomplicate matters. – John Kugelman Mar 18 '21 at 08:46
  • Worked. Thank you @JohnKugelman Also I had to use **fromdos** to convert dos to UNIX, since my text file was in dos – Vinisha Venugopal Mar 18 '21 at 22:14