LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   unique command - variation on the theme (https://www.linuxquestions.org/questions/linux-newbie-8/unique-command-variation-on-the-theme-867810/)

danielbmartin 03-10-2011 05:49 PM

unique command - variation on the theme
 
I have a large file and want to keep certain lines based on the first word in each. The words are blank-delimited character strings.

The first words are not necessarily unique.
I want to keep the uniques, and that's easy enough.
Where duplicates are encountered I want to keep only one
of each, and the kept line must be the *last* of the duplicates.

Sample input file:
ALBERT 54
BENJAMIN 37
BILL 24
BILL 25
BILL 77
CARL 40
CARL 44
CHESTER 59
DAVID 23
DAVID 23
DAVID 28
DAVID 61
EDGAR 33
EDWARD 54
EDWARD 59
EDWIN 30

Desired output file:
ALBERT 54
BENJAMIN 37
BILL 77
CARL 44
CHESTER 59
DAVID 61
EDGAR 33
EDWARD 59
EDWIN 30

I'm a newbie and still learning the basics, so please:
- no awk
- no bash
- no Perl

Let's stick to commands such as uniq, sort, sed, grep, cut, paste, etc.

danielbmartin 03-10-2011 08:32 PM

Quote:

Originally Posted by danielbmartin (Post 4286187)
I have a large file and want to keep certain lines based on the first word in each. The words are blank-delimited character strings.

The first words are not necessarily unique.
I want to keep the uniques, and that's easy enough.
Where duplicates are encountered I want to keep only one
of each, and the kept line must be the *last* of the duplicates.

Sample input file:
ALBERT 54
BENJAMIN 37
BILL 24
BILL 25
BILL 77
CARL 40
CARL 44
CHESTER 59
DAVID 23
DAVID 23
DAVID 28
DAVID 61
EDGAR 33
EDWARD 54
EDWARD 59
EDWIN 30

Desired output file:
ALBERT 54
BENJAMIN 37
BILL 77
CARL 44
CHESTER 59
DAVID 61
EDGAR 33
EDWARD 59
EDWIN 30

I'm a newbie and still learning the basics, so please:
- no awk
- no bash
- no Perl

Let's stick to commands such as uniq, sort, sed, grep, cut, paste, etc.

Got it!

tac < {InFile} | sort -k1,1 -t' ' -u

hda7 03-11-2011 09:07 AM

Quote:

Originally Posted by danielbmartin (Post 4286306)
Got it!

tac < {InFile} | sort -k1,1 -t' ' -u

You may want to add "-s" to your sort command. If I am correct, this ensures that lines that compare equal are never swapped in the sorting process.

colucix 03-12-2011 10:27 AM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


All times are GMT -5. The time now is 06:36 PM.