简体   繁体   中英

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)?

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

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

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM