Search code examples
linuxbashsortingcommand-lineuniq

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


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?


Solution

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

    sort -u fileA fileB | sort -k2 > fileC