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. 【USACO 3.1.4】形成的区域

    [描述]         N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张宽为A长为B的白纸上.这些长方形被放置时,保证了它们的边于白纸的边缘平行.所有的长方形都 ...

  2. php中session的运行机制

    在PHP中session默认是以文件的形式存储于服务器的 而客户端和服务端则是通过session_id来完成握手的,默认情况下PHP会将session_id存储于cookie中,用户每次请求时该ses ...

  3. Ubuntu12.04安装java以及Eclipse和Tomcat

    阔别已久的Java,现在捡起来偶感觉亚历山大啊,就单单一个环境的安装就搞得我焦头烂额啊.真后悔当初学习Java的时候没有记录下来这一门槛——环境的搭建,要知道学好一门语言,Develop Enviro ...

  4. JavaScript-学习一获取表单的值

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>he ...

  5. Groovy创建和解析json

    正文:  在Groovy 1.8发布新闻中,提到Groovy增加了对JSON的支持.Dustin Marx在其博文中,讲述了这一功能的使用. 用法真的很简单,创建一个JSON对象: import gr ...

  6. 通过 Xshell 5 连接 centOS 7 服务器

    一. 在安装好了centOS 7 的服务上,打开终端 运行 ip -s addr 命令 获取服务的IP地址 [root@localhost ~]# ip -s addr1: lo: <LOOPB ...

  7. Js点餐加减数量

    <button class="add-on" onclick="chgNum(1,'del')" ><i class="icon-m ...

  8. 【leetcode】1. Two Sums

    题目 https://leetcode.com/problems/two-sum/   Given an array of integers, find two numbers such that t ...

  9. C++学习笔记4——类的封装(2)

    简介: 重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成.其中一元运算符有一个参数,二元运算符有两个参数. 可以被重载的运算符 + - * / % ^ ...

  10. IOS 多个UIImageView 加载高清大图时内存管理

    IOS 多个UIImageView 加载高清大图时内存管理 时间:2014-08-27 10:47  浏览:59人 当我们在某一个View多个UIImageView,且UIImageView都显示的是 ...