user2617138
user2617138

Reputation: 1

find duplicate data between files in unix

I have 2 files with contents:-

file1:

918802944821    919968005200    kushinagar
919711354546    919211999924    delhi
915555555555    916666666666    kanpur
919711354546    915686524578    hehe
918802944821    4752168549  hfhkjh

file2:-

919211999924    919711354546    ghaziabad
919999999999    918888888888    lucknow
912222222222    911111111111    chandauli
918802944821    916325478965    hfhjdhjd

Now notice that number1 and number2 are interchanged in file1 and file2. I want to print only this duplicate line on the screen. to be more specific i want only the numbers or line to be printed on the screen which are duplicate like 8888888888 and 7777777777 are duplicate in the two files. I want only these two numbers on the screen or the whole line on the screen..

Upvotes: 0

Views: 60

Answers (2)

anubhava
anubhava

Reputation: 785058

Using awk you can do:

awk 'FNR==NR{a[$1,$2]++;next} a[$2,$1]' f1 f2
7777777777    8888888888    pqr

EDIT: Based on your edited question you can do:

awk 'FNR==NR{a[$1]++;b[$2]++;next} a[$1] || b[$1] {print $1} a[$2] || b[$2]{print $2}' f1 f2
919211999924
919711354546
918802944821

Upvotes: 1

Kent
Kent

Reputation: 195039

kent$  awk 'NR==FNR{a[$2 FS $1]=1;next}a[$1 FS $2]{print $1,$2}' f1 f2   
7777777777 8888888888

Upvotes: 0

Related Questions