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 -d : /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
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
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的更多相关文章
- sbt公布assembly解决jar包冲突 deduplicate: different file contents found in the following
一个.问题定义 近期使用sbt战斗assembly发生故障时,包,在package什么时候,发生jar包冲突/文件冲突,两个相同class来自不同jar包classpath内心冲突. 有关详细信息:我 ...
- 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 ...
- Git CMD - add: Add file contents to the index
命令格式 git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [- ...
- 【转载】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 ...
- 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. ...
- Chapter 1 Secondary Sorting:Introduction
开始学习<数据算法:Hadoop/Spark大数据处理技巧>第1-5章,假期有空就摘抄下来,毕竟不是纸质的可以写写画画,感觉这样效果好点,当然复杂的东西仍然跳过.写博客越发成了做笔记的感觉 ...
- 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 ...
- 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 ...
- PAT_A1028#List Sorting
Source: PAT A1028 List Sorting (25 分) Description: Excel can sort records according to any column. N ...
随机推荐
- cas sso入门(转)
转:http://blog.csdn.net/frinder/article/details/7969925 一.教程说明 前言 教程目的:从头到尾细细道来单点登录服务器及客户端应用的每个步骤 单点登 ...
- 正确设置网站title、keywords、description(转载)
本文转载自蚂蚁HR(www.mayihr.com) 优化技巧是老师在课堂上教不了你的,而自己也不可能在练习中领悟,最便捷的方法就是听取别人的经验,所以转载一下 1.title(网站标题) title, ...
- underscorejs-min学习
2.16 min 2.16.1 语法: _.min(list, [iteratee], [context]) 2.16.2 说明: 返回list中的最小值. list为集合,数组.对象.字符串或arg ...
- 使用wrapper将java程序注册程windows服务后不生效
使用wrapper将java程序注册程windows服务后不生效 使用add.bat或test***.bat测试通过了, 然后使用install***.bat注册后cmd显示注册成功. 但是程序到了运 ...
- angularJS内置指令一览
基础ng指令 ng-href ng-src ng-disabled ng-readonly ng-checked ng-selected ng-class ng-style ng-show ng-hi ...
- php 面向对象编程实例 __construct 和 __destruct 区别
大多数类都有一种称为构造函数的特殊方法.当创建一个对象时,它将自动调用构造函数,也就是使用new这个关键字来实例化对象的时候自动调用构造方 法.构 造函数的声明与其它操作的声明一样,只是其名称必须是_ ...
- HTML&CSS基础学习笔记1.32-选择器是什么
选择器是什么 选择器是CSS样式为了定位页面上的任意元素的一种方法. 选择器主要分为:元素标签选择器.通用选择器.类选择器.ID选择器.属性选择器.组合选择器.伪类选择器.伪元素选择器. 先做个了解, ...
- python种的builtin函数详解-第三篇
exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] eval(e ...
- Spark笔记--使用Maven编译Spark源码(windows)
1. 官网下载源码 source code,地址: http://spark.apache.org/downloads.html 2. 使用maven编译: 注意在编译之前,需要设置java堆大小以及 ...
- xcode5 自定义模板
经过一番周折,终于在xcode5上实现了一个简单的自定义模板,在项目中集成NSLogger库(增强NSLog的功能,https://github.com/fpillet/NSLogger)——新建项目 ...