Looking for a linux command - concatenate two files - sort by last name and delete duplicates - store in new file

72 Views Asked by At

I'm trying to concatenate two files, sort them by last name, delete duplicates and store them in a new file.

File's: "firstName lastName"

FileA + FileB --> FileC

I tried it with the sort command:

sort -uk2 fileA fileB > fileC

The problem is that this command deletes names with the same last name but diffrent first name.

"Hans Smith" + "Hans Smith" --> only one "Hans Smith" should remain. "Friedrich Bauer" + "Colin Bauer" --> should both be kept.

Any ideas?

2

There are 2 best solutions below

1
M. Nejat Aydin On BEST ANSWER

First sort the whole lines and remove duplicates then sort by the second field:

sort -u fileA fileB | sort -k2 > fileC
0
Ted Lyngmo On

You can split up sorting and removing duplicates in two operations. First sort on -k2, then use uniq to remove duplicates.

sort -k2 fileA fileB | uniq > fileC

Or, sort on the first name too:

sort -u -k2 -k1 fileA fileB > fileC