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 ...
随机推荐
- 在VC6中使用ogre进行游戏开发
微软公司开发的visual c++6.0堪称史上最易用.最成熟的开发工具.vc6以其小巧.轻便赢得了程序员的喜爱,以至于在VS大行其道的时代,很多程序员仍然使用vc6作为开发工具,vc6的欢迎性可见一 ...
- 超轻型响应jQuery旋转木马幻灯片插件anoSlide
http://www.jqcool.net/demo/201409/jquery-anoslide/
- CSS3圆角详解
一.CSS3圆角的优点 传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点: * 减少维护的工作量.图片文件的生成.更新. ...
- [Struts2学习笔记] -- 简单的类型转换
接下来学习一下Struts2简单的类型转换,Struts2基于ognl.jar实现了简单类型的数据转换.比如jsp页面中的form值与字段值的转换,下面写一个例子. 1.创建一个jsp页面,编写一个f ...
- Python中函数参数传递问题
先上两个例子: http://python.jobbole.com/85231/ a = 1 def fun(a): a = 2 fun(a) print a # 结果为1 fun(a)中的a,可以看 ...
- 清橙A1484
http://www.tsinsen.com/ViewGProblem.page?gpid=A1484### 题解: 在线插入并不好做,我们将所有操作离线,变为删除操作. 每次询问的时候对于当前B串所 ...
- xcode 发展史 及 做iOS 必须知道的小知识
Xcode 3.0 是开发人员建立 Mac OS X 应用程序的最快捷方式,也是利用新的苹果电脑公司技术的最简单的途径.Xcode 3.0 将Mac OS X的轻松使用,UNIX 能量以及高性能的开发 ...
- 7.1.1.关闭WebSocket连接
7.1.定义 7.1.1.关闭WebSocket连接 为_关闭WebSocket连接_,端点需关闭底层TCP连接.端点应该使用一个方法完全地关闭TCP连接,以及TLS会话,如果合适,丢弃任何可能已经接 ...
- 2015第30周三Spring常用工具类
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- 在 Windows 下远程桌面连接 Linux - XManager 篇
XManager是一个简单易用的高性能的运行在Windows平台上的X-Server软件,而Gnome和KDE就是X-Client,Linux下的X-Server则为Xorg.它能把远端Unix/Li ...