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  -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 -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  -d : /etc/passwd 

[root@rhel7 ~]# cut -f  -d : /etc/passwd |sort -n

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  -d : /etc/passwd |sort -rn

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:::root:/root:/bin/bash
rusky:x:::rusky:/home/rusky:/bin/bash
operator:x:::operator:/root:/sbin/nologin
bin:x:::bin:/bin:/sbin/nologin
games:x:::games:/usr/games:/sbin/nologin
ftp:x:::FTP User:/var/ftp:/sbin/nologin
avahi-autoipd:x:::Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
shutdown:x:::shutdown:/sbin:/sbin/shutdown
halt:x:::halt:/sbin:/sbin/halt
sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mail:x:::mail:/var/spool/mail:/sbin/nologin
dbus:x:::System message bus:/:/sbin/nologin
postfix:x::::/var/spool/postfix:/sbin/nologin
polkitd:x:::User for polkitd:/:/sbin/nologin
systemd-network:x:::systemd Network Management:/:/sbin/nologin
systemd-bus-proxy:x:::systemd Bus Proxy:/:/sbin/nologin
nobody:x:::Nobody:/:/sbin/nologin
[root@rhel7 ~]#

Sorting File Contents and Output with sort的更多相关文章

  1. sbt公布assembly解决jar包冲突 deduplicate: different file contents found in the following

    一个.问题定义 近期使用sbt战斗assembly发生故障时,包,在package什么时候,发生jar包冲突/文件冲突,两个相同class来自不同jar包classpath内心冲突. 有关详细信息:我 ...

  2. Xamarin 示例Standard Controls报错:xamarin Failed to compile interface file. See Build Output for details

    Standard Controls 示例下载地址: http://developer.xamarin.com/content/StandardControls/ Xamarin官网上的IOS示例“St ...

  3. Git CMD - add: Add file contents to the index

    命令格式 git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [- ...

  4. 【转载】Save terminal output to a file

    To write the output of a command to a file, there are basically 10 commonly used ways. Overview: Ple ...

  5. CareerCup Chapter 9 Sorting and Searching

    9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. ...

  6. Chapter 1 Secondary Sorting:Introduction

    开始学习<数据算法:Hadoop/Spark大数据处理技巧>第1-5章,假期有空就摘抄下来,毕竟不是纸质的可以写写画画,感觉这样效果好点,当然复杂的东西仍然跳过.写博客越发成了做笔记的感觉 ...

  7. JVM Specification 9th Edition (4) Chapter 4. The class File Format

    Chapter 4. The class File Format Table of Contents 4.1. The ClassFile Structure 4.2. Names 4.2.1. Bi ...

  8. file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes

    $>emtry_or_create_a_file.f $ll>>append_a_file.f standard output input error $ls -l /usr/bin ...

  9. PAT_A1028#List Sorting

    Source: PAT A1028 List Sorting (25 分) Description: Excel can sort records according to any column. N ...

随机推荐

  1. HTTP 状态响应码 意思详解/大全

    HTTP 状态响应码 意思详解/大全 转:http://blog.csdn.net/helin916012530/article/details/29842595 HTTP状态码(HTTP Statu ...

  2. 二十分钟弄懂C++11 的 rvalue reference (C++ 性能剖析 (5))

    C++ 11加了许多新的功能.其中对C++性能和我们设计class的constructor或assignment可能产生重大影响的非rvalue reference莫属!我看了不少资料,能说清它的不多 ...

  3. javascript之attribute 和 property

    首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...

  4. 如果使用的是orm,是否还需要关系索引

    简而言之:是的,仍然需要理解索引,即使是使用对象关系映射(ORM)工具. ORM工具能够产生符合逻辑的,合法的查询(多数的时候),除非只是生成非常基本的查询(例如仅是根据主键查询的),否则它很难生成适 ...

  5. Git对于单个文件的分批提交方式的使用

    很多时候,对于一个大的文件,可能有的同学改完之后不想一次提交,想分批提交.但这个时候由于git add的机制往往add之后就是整个一个文件被放到stage区了,这个时候肯定会想能不能对一个文件可以进行 ...

  6. 初涉JavaScript模式 (2) : 基本技巧

    尽量少用全局变量 大量使用全局变量会导致的后果 全局变量创建以后会在整个JavaScript应用和Web页面中共享.所有的全局变量都存在于一个全局命名空间内,很容易发生冲突 不知不觉创建了全局变量 其 ...

  7. websphere内存溢出,手动导出was的phd和javacore文件

    网上有很多方法,ibm官方也提供了.但是,好奇怪,好像只有百度博客的一片文章提出要先设置环境条目或定制属性,否则命令不生效. 所以,转载博客的时候,你最好自己尝试一下,要不然你就是在害人害己!我测试了 ...

  8. 【leetcode】62. Uniqe Paths

    题目 https://oj.leetcode.com/problems/unique-paths/   A robot is located at the top-left corner of a m ...

  9. USACO1.5 Checker Challenge(类n皇后问题)

    B - B Time Limit:1000MS     Memory Limit:16000KB     64bit IO Format:%lld & %llu   Description E ...

  10. IOS--UIAlertView的使用方法详细

    IOS--UIAlertView的使用方法详细   // UIAlertView的常用方法 // 标准样式 UIAlertView *oneAlertView = [[UIAlertView allo ...