Home > Software engineering >  Looking for a linux command - concatenate two files - sort by last name and delete duplicates - stor
Looking for a linux command - concatenate two files - sort by last name and delete duplicates - stor

Time:11-13

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?

CodePudding user response:

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

CodePudding user response:

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

sort -u fileA fileB | sort -k2 > fileC
  • Related