简体   繁体   English

Unix命令“ uniq”和“ sort”

[英]Unix command “uniq” & “sort”

As we known 如我们所知

uniq [options] [file1 [file2]]

It remove duplicate adjacent lines from sorted file1. 它从已排序的file1中删除重复的相邻行。 The option -c prints each line once, counting instances of each. 选项-c将每行打印一次,对每行的实例计数。 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: 并使用“ sort -1knr”对上述数据进行排序,结果如下:

   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: 您也可以使用sed ,如下所示:

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): 这也可以通过简化的正则表达式来完成(由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? 为什么使用uniq -c打印出现的次数,然后又想通过一些割/ awk / sed舞来删除?

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 ? 还是某些系统带有不支持-usort版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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