Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix uniq, sort & cut command remove duplicate lines

If we have the following result:

Operating System,50
Operating System,40
Operating System,30
Operating System,23
Data Structure,87
Data Structure,21
Data Structure,17
Data Structure,8
Data Structure,3
Crypo,33
Crypo,31
C++,65
C Language,39
C Language,19
C Language,4
Java 1.6,16
Java 1.6,11
Java 1.6,10
Java 1.6,2

I only want to compare the first field (book name), and remove duplicate lines except the first line of each book, which records the largest number. So the result is as below:

Operating System,50
Data Structure,87
Crypo,33
C++, 65
C Language,39
Java 1.6,16

Can anyone help me out that how could I do using uniq, sort & cut command? May be using tr, head or tail?

like image 407
eleven Avatar asked Dec 13 '25 19:12

eleven


1 Answers

Most elegant in this case would seem

rev input | uniq -f1 | rev
like image 64
sehe Avatar answered Dec 16 '25 01:12

sehe