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 ...
随机推荐
- C++中L和_T()之区别(转)
C++中L和_T()之区别 分类: C/C++2011-01-12 11:45 2878人阅读 评论(1) 收藏 举报 c++编译器apic 字符串前面加L表示该字符串是Unicode字符串._T是一 ...
- 利用Php ssh2扩展实现svn自动提交到测试服务器
1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...
- Python 网路编程读书笔记x UDP
UDP 协议基础 在IP网络层,所有的数据包会向一个指定的主机传输 Source IP -> Destination IP 但是两台机器之间可能有许多独立的应用需要进行通信,因此为了区分不同的 ...
- WebApi 4.0 默认方式不支持HttpGet 请求
如果Controller方法中没有指定请求方式,在RC版本中默认是HttpPost ,Beta版本中支持所有方法GET, PUT, POST and Delete,而在RC版本后做了改变只支持Http ...
- Android中AsyncTask异步
今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...
- JS 输出与变量
1. JS的输出 innerHTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- 通过ctypes获得python windows process的内存使用情况
通过ctypes 类库中的win32方法GetProcessMemoryInfo()获得当前进程的内存使用情况.该函数可以在32或者64位,python2.6+及python3.x之上都能有用. &q ...
- Android 之 WebView
1:在AndroidManifest.xml中添加允许android访问网络权限. <uses-permission android:name="android.permission. ...
- 关于function与closure
function 方式 scope function closure expression anonymous function class(this, prototype)
- TProcedure,TMethod,TNotifyEvent,TWndMethod的区别,并模拟点击按钮后发生的动作
忽然发现TProcedure和TNotifEvent的区别还挺大的: procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage ...