Linux sort 实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Guoxiang1030_/article/details/102574405

1.1 目标

  • sort可针对文本文件的内容,以行为单位来排序。

1.2 路径

  • 第一步: 对字符串排序
  • 第二步: 去重排序
  • 第三步: 对数值排序
  • 第四步: 对成绩排序

2.3 实现

第一步: 对字符串排序

[root@node01 tmp]# cat 2.txt
banana
apple
pear
orange
pear

[root@node01 tmp]# sort 2.txt 
apple
banana
orange
pear
pear

第二步: 去重排序

参数 英文 含义
-u unique 去掉重复的

它的作用很简单,就是在输出行中去除重复行。

[root@node01 tmp]# sort -u 2.txt 
apple
banana
orange
pear

第三步: 对数值排序

参数 英文 含义
-n numeric-sort 按照数值大小排序
-r reverse 使次序颠倒
  • 准备数据

    [root@node01 tmp]# cat 3.txt 
    1
    3
    5
    7
    11
    2
    4
    6
    10
    8
    9
    
  • 默认按照字符串排序

    [root@node01 tmp]# sort 2.txt 
    1
    10
    11
    2
    3
    4
    5
    6
    7
    8
    9
    
  • 升序

    [root@node01 tmp]# sort -n 2.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
  • 倒序

    [root@node01 tmp]# sort -n -r 2.txt
    11
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    
  • 合并式

    [root@node01 tmp]# sort -nr 2.txt  
    11
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    

    第四步: 对成绩排序

参数 英文 含义
-t field-separator 指定字段分隔符
-k key 根据那一列排序
# 根据第二段成绩 进行倒序显示 所有内容
sort -t ',' -k2nr score.txt 

猜你喜欢

转载自blog.csdn.net/Guoxiang1030_/article/details/102574405