Difference between 2 files, one with 1 column and 2nd file with multiple columns

Hi,
I need to find the difference between 2 files in unix and write the result in the new file

File1:

A
B

File2:

X  123  hajkd
Y  345  adjfka
A  123   djafjhd
B  678   dsndjks

Output file:

X  123  hajkd
Y  345  adjfka

Thanks.

awk 'FNR==NR {f1[$0];next} !($1 in f1)' file1 file2
1 Like

iam new to unix
in which file is it writing the results, can you please explain,

it should output to the screen. You can redirect that to a file of your choice:

awk 'FNR==NR {f1[$0];next} !($1 in f1)' file1 file2 > myOutputFile
1 Like

vgersh99 THanks A Lot , Its worked.

recevied
abc
def
ghi

totallist
abc 123 jasdhfaj
def 345 fjdgkfsfh
ghi 567 dfjdhdhfj
jkl 678 djkahfdjshdf
xyz 984 jdfdhfhdh

myOutputFile
jkl 678 djkahfdjshdf
xyz 984 jdfdhfhdh

I used this command for the output : awk 'FNR==NR {f1[$0];next} !($1 in f1)' recevied totallist > myOutputFile

Can any one explain the command, its hard to understand, why they used '$0' in first and '$1' in the second .

hey,

i dont know how to open a new thread , hence posting here, coz my question is related.
i want to compare 2 similar files. if the contents are same then i want to echo "no new data added OR files are same". if some new data is added, or there is difference in file, i want to print on difference , also save the difference in some third file.

[code]
file a:
name : xyz
age : ab
address : pqr, st
profession : engg

file b:
name : xyz
age : ab
address : pqr, st
profession : engg ETC // extra words added wrt file a
job : project engg // extra line added wrt file a

output file c :
profession : engg ETC // diff part is highlighted to differentiate to file a
job : project engg // extra line printed, since added line wrt file a

[\code]

Please enlighten.

Thanks and Regards,
Rishi