Pick a line in file 2 basing on array in file1

Dear friends,

I have two files. One with all IDs(in a single field) . And another with data(of which say field 5 is ID). I want to create an array of IDs using first file and
while reading second file if the ID appears in the array I need to print $0 else skip.

After a long gap I am working again in awk.

My code is:

FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" { ID=$5;
IF (ID IN VALIDID) PRINT }

Your help is highly appreciated.

PND

try:

awk -f 1.awk ID.TXT DATA.TXT

where 1.awk :

FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" { ID=$5;
if (ID in VALIDID) print }

not sure why you want to use awk but you could simply use join.

... or grep -f ID.TXT DATA.TXT ?

Than you. This worked. But one thing I wanted to know,
my code looks fine, but returns nothing. If someone can help me
understand how array is handled here, it will help me with other such
situations as well.

You shouldn't be using caps in the awk script.

--ahamed

Try

FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" && ($5 in VALIDID)