Zenet
Zenet

Reputation: 7431

Differences/merging two files

I have two lists of IP addresses. I need to merge them into three files, the intersection, those from list1 only and those from list2 only.

can I do this with awk/diff or any other simple unix command? How?

The files look like this:

111.222.333.444
111.222.333.445
111.222.333.448

Thank you!

Upvotes: 5

Views: 317

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360683

If the files are sorted then

join list1 list2

will output the intersection.

join -v 1 list1 list2

will output the ones that are in list1 only.

join -v 2 list1 list2

will output the ones that are in list2 only.

Upvotes: 3

Brandon Horsley
Brandon Horsley

Reputation: 8114

First sort them, using sort, and then you can use comm.

Intersection: comm -12 <file1> <file2>

List 1 Only: comm -23 <file1> <file2>

List 2 Only comm -13 <file1> <file2>

Upvotes: 2

Related Questions