How to loop through a list and pass as variable in bash / awk

I have a config.file in which I define a few variable that are ultimately called in a different script.

e.g.: in a config.file, I have ref=L42023.1, which I call as $ref in a script.sh. Now, I have a huge list of ref values stored in a single column of an input file:

$cat input.file
1 A L41223.2
2 B D45023.5
3 C L42823.6

Is it possible to loop through this list, i.e. run the script.sh with the first entry of the 3rd field (L41223.2), once completed, run it again with the second entry (D45023.5) etc. The $ref is called in a burrow-wheeler aligner in my script, such as:

bwa index -p INDEX $ref.fasta

The resulting value would be L41223.2.fasta

I have three different variables in my config.file that are used in my script.sh, but I guess the same principle applies.

Many thanks in advance.

1 answer

  • answered 2018-06-19 20:43 Cyrus

    With GNU xargs:

    cut -d " " -f 3 input.file | xargs -I{} echo bwa index -p INDEX {}.fasta
    

    or

    awk '{print $3}' input.file | xargs -I{} echo bwa index -p INDEX {}.fasta
    

    Remove echo if output looks okay.

    Update:

    while read -r c1 c2 c3; do
      echo bwa index -p INDEX ${c3}.fasta
      # place rest of your script here
    done < input.file
    

    Remove echo if output looks okay.