compare 2 files

Hi,

I have 2 files as below:

file1 (take note that this is an 800MB-sized file)

111111,1111
222222,2222
333333,3333

and

file2 (4MB file)

111111
444444
333333

I need to compare 1st field of both files and produce field2, as below:

1111
3333

I tried this (based from another post):

awk -F"," 'FILENAME=="file1"{A[$1]=$1} FILENAME=="file2"{if(A[$1]){print}}'

and this (also based from another post):

nawk -F"," 'BEGIN{while(getline<"file1") a [$1]=1 } ; a [$1] ==1 {print $2 }'  file2

Both didn't work for me. :confused:

Hope you can help.

Thanks!

define "didn't work" - you got errors? your computer froze? what exactly happened? Also, you have file1 and file2 reversed in the logic. you read the smaller file first, then find matches in the bigger one.

awk -F , 'NR==FNR{a[$1];next} $1 in a{print $2}' file2 file1
1 Like
$ fgrep -f file2 file1 | cut -d',' -f2

This worked beautifully! Thanks!!!:b: