in reply to modifying the remove duplicates code

As you seem to be already doing, sort the file on the key(s) that you wish to compare, then compare each record to “the previous record” as before.   The sort verb allows you to provide a record-comparison function so that you can sort on multiple fields.

See also:   Text::Record::Deduper.   Yup, there’s a CPAN module for everything.