Passing variable to awk

Hi,
I'm new with this stuff, but I hope you can help me.

This is what I'm trying to do:

for id in $var; do

awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp

done

I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and print the second column to the file neigh.tmp

The problem is that writing $id in the awk command seems not working ...

Suggestions?

read the man page of awk. There is a -v option to pass the shell variables to awk.

Give it a try :slight_smile:

Please use code tags.
-v options works like this:

for id in $var; do
    awk -v i=$id '{if ($1 == i) print $2}' merg_data.dat > neigh.tmp
done

Or using awk's condition{action} semantics:

awk -v i=$id '$1==i{print $2}'

thanks, now it works!

:slight_smile: