awk arrays - compare value in second column to variable

Hello,

I am trying to redirect files to a directory by using a config file. The config files is as such:

xxxxxx,ID,PathToDirectory
xxxxxx,ID2,PathToDirectory2

and so on...

I have a variable that should match one of these IDs. I want to load this config file into an awk array, and then parse trhough it to find the record and then use the PathToDirectory to copy my file to it.

This is what I have:

awk -v ak2=$AK202 -F "," '{  configfile[NR] = $0   }
                  END { for(i = 1; i <= NR; i++) { if (configfile=ak2) print configfile  }  } ' $INPUTFILE

Any thoughts? My idea is to fill the array, and then compare my AK202 variable to the second column, store the third as the destination directory.

Regards!

echo "xxxxxx,10,pth1 xxxxxx,11,pth2 xxxxxx,12,pth3" | tr ' ' '\n' | awk 'BEGIN {FS=","} { if ($2 == 11) { A[5]=$3; print A[5] } } '

produces

pth2

Hi, one of the niceties of awk is that it knows associative arrays. This means that you are not limited to the use of integers to use as an index.. So instead of NR you can use $2 as the index, which would save you a loop. But in addition, in this case you do not even need an array, since you can evaluate straight away, while processing the lines..

awk -v ak2="$AK202" -F, '$2==ak2' "$INPUTFILE"

is all it takes. This solution would be more efficient:

awk -v ak2="$AK202" -F, '$2==ak2{print; exit}' "$INPUTFILE"

You can also use print $3 instead of print to only print the third field.