eleven
eleven

Reputation: 369

Unix command "uniq" & "sort"

As we known

uniq [options] [file1 [file2]]

It remove duplicate adjacent lines from sorted file1. The option -c prints each line once, counting instances of each. So if we have the following result:

     34 Operating System
    254 Data Structure
      5 Crypo
     21 C++
   1435 C Language
    589 Java 1.6

And we sort above data using "sort -1knr", the result is as below:

   1435 C Language
    589 Java 1.6
    254 Data Structure
     34 Operating System
     21 C++
      5 Crypo

Can anyone help me out that how to output only the book name in this order (no number)?

Upvotes: 2

Views: 8218

Answers (3)

mivk
mivk

Reputation: 14834

Why do you use uniq -c to print the number of occurences, which you then want to remove with some cut/awk/sed dance?

Instead , you could just use

sort -u $file1 $file2 /path/to/more_files_to_glob*

Or do some systems come with a version of sort which doesn't support -u ?

Upvotes: 0

You can also use sed for that, as follows:

uniq -c filename | sort -k -1nr | sed 's/[0-9]\+ \(.\+\)/\1/g'

Test:

echo "34 Data Structure" | sed 's/[0-9]\+ \(.\+\)/\1/g'
Data Structure

This can also be done with a simplified regex (courtesy William Pursell):

echo "34 Data Structure" | sed 's/[0-9]* *//'
Data Structure

Upvotes: 2

Barmar
Barmar

Reputation: 780984

uniq -c filename | sort -k 1nr | awk '{$1='';print}'

Upvotes: 2

Related Questions