zoukankan      html  css  js  c++  java
  • Sorting File Contents and Output with sort

     Sorting File Contents and Output with sort  

    Another very useful command to use on text file is  sort . As you can probably guess, this command sorts text. If you type  sort /etc/passwd , for instance, the content of the /etc/passwd file is sorted in alphabetic order. You can use the  sort  command on the output of a command also, as in  cut -f 1 -d : /etc/passwd | sort , which sorts the contents of the first column in the /etc/passwd file. 

    [root@rhel7 ~]# cut -f 1 -d : /etc/passwd 
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    avahi-autoipd
    systemd-bus-proxy
    systemd-network
    dbus
    polkitd
    tss
    postfix
    sshd
    rusky
    [root@rhel7 ~]# cut -f 1 -d : /etc/passwd | sort
    adm
    avahi-autoipd
    bin
    daemon
    dbus
    ftp
    games
    halt
    lp
    mail
    nobody
    operator
    polkitd
    postfix
    root
    rusky
    shutdown
    sshd
    sync
    systemd-bus-proxy
    systemd-network
    tss


    By default, the  sort  command sorts in alphabetic order(默认是以阿尔法字母排序). In some cases, that is not convenient because the content that needs sorting may be numeric or in another format. The  sort  command offers different options to help sorting these specific types of data. Type, for instance,  cut -f 3 -d : /etc/passwd | sort -n  to sort the third field of the /etc/passwd file in numeric order.(使用sort -n 参数来按数字进行排序)

    [root@rhel7 ~]# cut -f 3 -d : /etc/passwd 
    0
    1
    2
    3
    4
    5
    6
    7
    8
    11
    12
    14
    99
    170
    999
    998
    81
    997
    59
    89
    74
    1000
    [root@rhel7 ~]# cut -f 3 -d : /etc/passwd |sort -n
    0
    1
    2
    3
    4
    5
    6
    7
    8
    11
    12
    14
    59
    74
    81
    89
    99
    170
    997
    998
    999
    1000

    It can be useful also to sort in reverse order; if you use the command  du -h | sort -rn , you get a list of files sorted with the biggest file in that directory listed first. (按倒序排序)

    [root@rhel7 ~]# cut -f 3 -d : /etc/passwd |sort -rn
    1000
    999
    998
    997
    170
    99
    89
    81
    74
    59
    14
    12
    11
    8
    7
    6
    5
    4
    3
    2
    1
    0

      You can also use the  sort  command and specify which column you want to sort. To do this, use  sort -k3 -t : /etc/passwd , for instance, which uses the field separator : to sort the third column of the /etc/passwd file.   (按第三列进行排序,-t指定分隔符为:)

    [root@rhel7 ~]# sort -k3 -t : /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    rusky:x:1000:1000:rusky:/home/rusky:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    bin:x:1:1:bin:/bin:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    polkitd:x:997:995:User for polkitd:/:/sbin/nologin
    systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
    systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    [root@rhel7 ~]# 
  • 相关阅读:
    强类型DataSet (2011-12-30 23:16:59)转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 http://blog.sina.com.cn/s/blog_9d90c4140101214w.html
    整合91平台接入的ANE
    keychain不能导出p12证书的解决方法
    制作IOS ANE的基本流程
    SVN 提交失败 非LF行结束符
    ANE打包工具使用视频教程 -- 梦宇技术 @极客学院
    RSA算法原理
    IOS 之 NSBundle 使用
    iOS编程——Objective-C KVO/KVC机制
    视图横竖屏控制技巧
  • 原文地址:https://www.cnblogs.com/rusking/p/5590361.html
Copyright © 2011-2022 走看看