Arun
Arun

Reputation: 1220

Unix : Compare two TXT file issue and keep the lines which is not unique

I have two TXT files as

#1.txt
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
5.5.5.5

#2.txt
1.1.1.1
2.2.2.2
6.6.6.6
7.7.7.7
8.8.8.8

I made comparison of 1.txt to 2.txt and get the IPs which is not in 2.txt when comparing with 1.txt . I did,

#comm -2 -3 <(sort 1.txt) <(sort 2.txt) > Out.txt

Output

#Out.txt
3.3.3.3
4.4.4.4
5.5.5.5

The Out.txt IPs are the IPs not in 2.txt file.

Now,I would like compare 1.txt against 2.txt and get the one which is not unique from 1.txt .

#1.txt
1,1.1.1.1
2,2.2.2.2
3,3.3.3.3
4,4.4.4.4
5,5.5.5.5

#2.txt
1.1.1.1
2.2.2.2
6.6.6.6
7.7.7.7
8.8.8.8

Expected Result

#Out.txt
3,3.3.3.3
4,4.4.4.4
5,5.5.5.5

How to include IDs in my output result ?

Upvotes: 0

Views: 73

Answers (1)

Kent
Kent

Reputation: 195199

this awk one-liner should work for your example:

awk -F, 'NR==FNR{a[$0];next}!($2 in a)' 2.txt 1.txt

Upvotes: 1

Related Questions