Found and Notfound compare two file

File1

site1.111.com
site1.333.com
site1.555.uk

file2

site1.222.test
site1.111.com
site1.333.com
site1.444.test
site1.555.uk

Need Result

site1.222.test,Notfound
site1.111.com,found
site1.333.com,found
site1.444.com,Notfound
site1.555.uk,found

I was trying to do using for loop but i cant update the notfound comment

Can you post your for loop code? We can help you fix your code.

Hello ranjancom2000,

Could you please try following and let me know if this helps you.

awk 'FNR==NR{a[$0];next} {printf("%s\n",$0 in a?$0",found":$0",Notfound")}'   Input_file1  Input_file2

Output will be as follows.

222,Notfound
111,found
333,found
444,Notfound
555,found

Thanks,
R. Singh

Hi Ravin,

It was printing for all lines notfound

My first question would be, is the input for "file1" and "file2" representative of your actual input? e.g. Ravinder's solution is working on the entire line of input. If the data you've shown is only part of the line, it won't work if the rest of the line does not match in both files.

Also, as Yoda suggests, post your for-loop code. Perhaps we could help you understand why that isn't working.

Also, try Ravinder's suggestion again but this time with $1 instead of $0 for all occurrences.

Hi Scott,

I have update the file in thread one it will be similar to that

Hello ranjancom2000,

My same code is working for your newly shown Input_file(s), please do let us know what is not working(with complete details). Simply saying not working will not help us to help you and try to show us all Input_file sample in a single post itself.

awk 'FNR==NR{a[$0];next} {printf("%s\n",$0 in a?$0",found":$0",Notfound")}' FiLE1  FiLE2
site1.222.test,Notfound
site1.111.com,found
site1.333.com,found
site1.444.test,Notfound
site1.555.uk,found

EDIT: In case your Input_file(s) have carriage returns in them which you could check by doing cat -v Input_file , if they have it and run following code:

awk 'FNR==NR{gsub(/\r/,"");a[$0];next} {gsub(/\r/,"");printf("%s\n",$0 in a?$0",found":$0",Notfound")}' FiLE1  FiLE2

Thanks,
R. Singh

1 Like

One reason any solution for the files in post#1 will fail is that file1 has DOS line terminators (<CR> <LF>), while file2 has *nix ones (just <LF>).

thanks it was working has some space

Sorry please ignore this. I have update on another post which is more related to it

I need to check the column 3 from file1 and compare it with Colume1 of file 1 and append.

File1

533 Server1 002D9 34526
533 Server1 002DA 34526
533 Server1 002DB 34526
533 Server1 002DC 34526
533 Server1 002DD 34526
533 Server1 002DE 34526
533 Server1 002DF 34526
533 Server1 002E1 34526
533 Server1 002E2 34526
533 Server1 002E3 34526
533 Server1 002E4 34526
533 Server1 002E5 34526
533 Server1 002E6 34526
533 Server1 002E7 34526

File2

002D9 RDFType:R1 RemoteDeviceSymmetrixName:001DD RemoteSymmetrixID:000296700555
002DA RDFType:R1 RemoteDeviceSymmetrixName:001DE RemoteSymmetrixID:000296700555
002DB RDFType:R1 RemoteDeviceSymmetrixName:001DF RemoteSymmetrixID:000296700555
002DC RDFType:R1 RemoteDeviceSymmetrixName:001E0 RemoteSymmetrixID:000296700555
002DD RDFType:R1 RemoteDeviceSymmetrixName:001E1 RemoteSymmetrixID:000296700555
002DE RDFType:R1 RemoteDeviceSymmetrixName:001E2 RemoteSymmetrixID:000296700555
002DF RDFType:R1 RemoteDeviceSymmetrixName:001E3 RemoteSymmetrixID:000296700555
002E0 RDFType:R1 RemoteDeviceSymmetrixName:001E4 RemoteSymmetrixID:000296700555
002E1 RDFType:R1 RemoteDeviceSymmetrixName:001E5 RemoteSymmetrixID:000296700555
002E2 RDFType:R1 RemoteDeviceSymmetrixName:001E6 RemoteSymmetrixID:000296700555
002E3 RDFType:R1 RemoteDeviceSymmetrixName:001E7 RemoteSymmetrixID:000296700555
002E4 RDFType:R1 RemoteDeviceSymmetrixName:001E8 RemoteSymmetrixID:000296700555
002E5 RDFType:R1 RemoteDeviceSymmetrixName:001E9 RemoteSymmetrixID:000296700555
002E6 RDFType:R1 RemoteDeviceSymmetrixName:001EA RemoteSymmetrixID:000296700555
002E7 RDFType:R1 RemoteDeviceSymmetrixName:001EB RemoteSymmetrixID:000296700555

Output required

533 Server1 002D9 34526 002D9 RDFType:R1 RemoteDeviceSymmetrixName:001DD RemoteSymmetrixID:000296700555
533 Server1 002DA 34526 002DA RDFType:R1 RemoteDeviceSymmetrixName:001DE RemoteSymmetrixID:000296700555
533 Server1 002DB 34526 002DB RDFType:R1 RemoteDeviceSymmetrixName:001DF RemoteSymmetrixID:000296700555
533 Server1 002DC 34526 002DC RDFType:R1 RemoteDeviceSymmetrixName:001E0 RemoteSymmetrixID:000296700555
533 Server1 002DD 34526 002DD RDFType:R1 RemoteDeviceSymmetrixName:001E1 RemoteSymmetrixID:000296700555
533 Server1 002DE 34526 002DE RDFType:R1 RemoteDeviceSymmetrixName:001E2 RemoteSymmetrixID:000296700555
533 Server1 002DF 34526 002DF RDFType:R1 RemoteDeviceSymmetrixName:001E3 RemoteSymmetrixID:000296700555
Notfound 002E0 RDFType:R1 RemoteDeviceSymmetrixName:001E4 RemoteSymmetrixID:000296700555
533 Server1 002E1 34526 002E1 RDFType:R1 RemoteDeviceSymmetrixName:001E5 RemoteSymmetrixID:000296700555
533 Server1 002E2 34526 002E2 RDFType:R1 RemoteDeviceSymmetrixName:001E6 RemoteSymmetrixID:000296700555
533 Server1 002E3 34526 002E3 RDFType:R1 RemoteDeviceSymmetrixName:001E7 RemoteSymmetrixID:000296700555
533 Server1 002E4 34526 002E4 RDFType:R1 RemoteDeviceSymmetrixName:001E8 RemoteSymmetrixID:000296700555
533 Server1 002E5 34526 002E5 RDFType:R1 RemoteDeviceSymmetrixName:001E9 RemoteSymmetrixID:000296700555
533 Server1 002E6 34526 002E6 RDFType:R1 RemoteDeviceSymmetrixName:001EA RemoteSymmetrixID:000296700555
533 Server1 002E7 34526 002E7 RDFType:R1 RemoteDeviceSymmetrixName:001EB RemoteSymmetrixID:000296700555

Hello ranjancom2000,

Could you please try following and let me know if this helps you.

awk 'FNR==NR{a[$3]=$0;next} {printf("%s %s\n",$1 in a?a[$1]:"Notfound",$0)}'  Input_file1   Input_file2

Thanks,
R. Singh

1 Like

I can able to resolve this from this post since sorry for that.