概述

sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中获取输入。


语法

sort (选项) (参数)

选项

 -b:忽略每行前面开始出的空格字符;
-c:检查文件是否已经按照顺序排序;
-d:排序时,处理英文字母、数字及空格字符外,忽略其他的字符;
-f:排序时,将小写字母视为大写字母;
-i:排序时,除了040至176之间的ASCII字符外,忽略其他的字符;
-m:将几个排序号的文件进行合并;
-M:将前面3个字母依照月份的缩写进行排序;
-n:依照数值的大小排序;
-o<输出文件>:将排序后的结果存入制定的文件;
-r:以相反的顺序来排序;
-t<分隔字符>:指定排序时所用的栏位分隔字符;
+<起始栏位>-<结束栏位>:以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。

参数

文件:指定待排序的文件列表。

官方指导sort –help / man sort

[root@entle2 ~]# sort --help
Usage: sort [OPTION]... [FILE]...or:  sort [OPTION]... --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.Mandatory arguments to long options are mandatory for short options too.
Ordering options:-b, --ignore-leading-blanks  ignore leading blanks-d, --dictionary-order      consider only blanks and alphanumeric characters-f, --ignore-case           fold lower case to upper case characters-g, --general-numeric-sort  compare according to general numerical value-i, --ignore-nonprinting    consider only printable characters-M, --month-sort            compare (unknown) < `JAN' < ... < `DEC'-h, --human-numeric-sort    compare human readable numbers (e.g., 2K 1G)-n, --numeric-sort          compare according to string numerical value-R, --random-sort           sort by random hash of keys--random-source=FILE    get random bytes from FILE-r, --reverse               reverse the result of comparisons--sort=WORD             sort according to WORD:general-numeric -g, human-numeric -h, month -M,numeric -n, random -R, version -V-V, --version-sort          natural sort of (version) numbers within textOther options:--batch-size=NMERGE   merge at most NMERGE inputs at once;for more use temp files-c, --check, --check=diagnose-first  check for sorted input; do not sort-C, --check=quiet, --check=silent  like -c, but do not report first bad line--compress-program=PROG  compress temporaries with PROG;decompress them with PROG -d--files0-from=F       read input from the files specified byNUL-terminated names in file F;If F is - then read names from standard input-k, --key=POS1[,POS2]     start a key at POS1 (origin 1), end it at POS2(default end of line)-m, --merge               merge already sorted files; do not sort-o, --output=FILE         write result to FILE instead of standard output-s, --stable              stabilize sort by disabling last-resort comparison-S, --buffer-size=SIZE    use SIZE for main memory buffer-t, --field-separator=SEP  use SEP instead of non-blank to blank transition-T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or /tmp;multiple options specify multiple directories-u, --unique              with -c, check for strict ordering;without -c, output only the first of an equal run-z, --zero-terminated     end lines with 0 byte, not newline--help     display this help and exit--version  output version information and exitPOS is F[.C][OPTS], where F is the field number and C the character position
in the field; both are origin 1.  If neither -t nor -b is in effect, characters
in a field are counted from the beginning of the preceding whitespace.  OPTS is
one or more single-letter ordering options, which override global ordering
options for that key.  If no key is given, use the entire line as the key.SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.With no FILE, or when FILE is -, read standard input.*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.Report sort bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'sort invocation'

栗子

sort将文件/文本的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。

[root@entel2 ~]# cat st.txt
aa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5
[root@entel2 ~]# sort st.txt
aa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
eee:50:5.5

科普下ASCII码:

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。

ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符。标准ASCII 码也叫基础ASCII码,使用7
位二进制数来表示所有的大写和小写字母,数字0 到9、标点符号, 以及在美式英语中使用的特殊控制字符。

32~126(共95个)是字符(32是空格),其中48~57为0到9十个阿拉伯数字。
65~90为26个大写英文字母,97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。

ASCII对照表:

http://tool.oschina.net/commons?type=4

网上也有很多ascii码转换器 可以利用。

ASCII大小规则

1)数字0~9比字母要小。如”7”<”F”;

2)数字0比数字9要小,并按0到9顺序递增。如”3”<”8”

3)字母A比字母Z要小,并按A到Z顺序递增。如”A”<”Z”

4)同个字母的大写字母比小写字母要小。如”A”<”a”。


忽略相同行使用-u选项或者uniq

[root@entel2 ~]# cat st.txt
aa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2
eee:50:5.5
eee:50:5.5
[root@entel2 ~]# sort -u st.txt
aa:10:1.1
bbb:20:2.2
ccc:30:3.3
ddd:40:4.4
eee:50:5.5
[root@entel2 ~]# uniq st.txt
aa:10:1.1
ccc:30:3.3
ddd:40:4.4
bbb:20:2.2 

sort的-n、-r、-k、-t选项的使用

-n:依照数值的大小排序;
-r:以相反的顺序来排序;
-k, –key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line)
-t<分隔字符>:指定排序时所用的栏位分隔字符;

将BB列按照数字从小到大顺序排列:

[root@entel2 ~]# cat st1.txt
AAA:BB:CC
aaa:30:1.6
ccc:50:3.3
ddd:20:4.2
bbb:10:2.5
eee:40:5.4
eee:60:5.1
[root@entel2 ~]# sort -nk 2 -t: st1.txt
AAA:BB:CC
bbb:10:2.5
ddd:20:4.2
aaa:30:1.6
eee:40:5.4
ccc:50:3.3
eee:60:5.1

将CC列数字从大到小顺序排列:

[root@entel2 ~]# sort -nrk 3 -t: st1.txt
eee:40:5.4
eee:60:5.1
ddd:20:4.2
ccc:50:3.3
bbb:10:2.5
aaa:30:1.6
AAA:BB:CC注意指定-n 和没有-n的区别 ,-n依照数值大小排序
[root@entel2 ~]# sort -rk 3 -t: st1.txt
AAA:BB:CC
eee:40:5.4
eee:60:5.1
ddd:20:4.2
ccc:50:3.3
bbb:10:2.5
aaa:30:1.6

分析:

-n是按照数字大小排序,
-r是以相反顺序,
-k是指定需要排序的栏位,
-t指定栏位分隔符为冒号

-k选项的具体语法格式



Linux-sort排序相关推荐

  1. linux文件名排序规则,Linux sort 排序使用详解

    前阵子,写脚本时,用到sort来对文件排序:第一次没有达到所需的效果,原来是LANG设置问题,后来先export LANG=C,然后再sort就满足我的要求了. 某牛人总结的sort用法:http:/ ...

  2. linux sort命令 排序,Linux sort排序方法

    在文件的操作过程中,因为文件过多,往往需要进行一下排序,排序方法也就是从小到大排序或者从大到小排序.比如我们从nginx日志中需要找到访问量最长的url,那就需要对请求时间进行一个排序,根据请求时间长 ...

  3. linux sort 排序 指定间隔符

    -k 指定列数字 -t 指定间隔符 # lotus net scores | sort -r -n -k 2 -t , 12D3KooWJTUBUjtzWJGWU1XSiY21CwmHaCNLNYn2 ...

  4. linux sort 排序命令简介

    语法 sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][ ...

  5. linux sort 排序 性能,Linux中sort 排序

    sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始!   1 sort的工作原理   sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依 ...

  6. linux排序语言,Linux sort 排序命令uniq去重复行

    sort命令 sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语 ...

  7. linux: sort排序数据 grep搜索数据

    sort 1.sort filename 输出排序后的结果,默认按字符大小排序 2.-n 按数字排序(如果内容是数字的话) 3.-M 按月份排序(如果是三字符简写月份的话) 下面这个例子非常实用: g ...

  8. linux sort排序及取前几条数据

    查看sort --help -n 根据字符串的数值进行比较 -k 根据某一个关键字的位置或者类型排序 -r 倒序排序 -t 字段分隔,后面跟分隔符 查看head --help -n 打印前几行记录,后 ...

  9. linux sort 排序 1 99,Linux操作系统中排序命令Sort的使用方法

    语法格式 sort [ -A ] [ -b ] [ -c ] [ -d ] [ -f ] [ -i ] [ -m] [ -n ] [ -r ] [ -u ] [ -o OutFile ] [ -t C ...

  10. linux sort排序工具和uniq去重工具使用示例

    sort工具 -r 降序排列: -o  保存到文件:

最新文章

  1. [整理] C#调用SQLDMO.DLL时间数据库备份 / 还原。 (香神无涯) // C#实现SQLSERVER2000数据库备份还原的两种方法 (带进度条)...
  2. 机器学习系列-随机过程
  3. java队列转集合_Java集合 使用Queue
  4. 入门Mac快捷键详细分类整理,包括Eclipse和Android Studio中一些常用的快捷键
  5. python基础教程(第二版)
  6. BZOJ 2436 NOI嘉年华(单调优化)
  7. hikari数据源配置类_Spring中的“多数据源”之详解
  8. ibatis mysql sqlmapconfig_iBATIS sqlMapConfig配置详解
  9. java读取gpx文件,从Leaflet导出GPX文件
  10. 设计模式学习笔记之装饰者模式
  11. Shiro笔记(二)Shiro集成SpringMVC的环境配置
  12. linux清理硬盘工具,linux磁盘清理方法 Linux 下垃圾清理工具 BleachBit
  13. python:网络数据收集
  14. 直播回顾 | BPM平台与微服务架构天生契合(附资料下载)_Nebulogy_纳比云
  15. 夏天来了,来吃鹅厂新瓜,小马哥已吃
  16. 惠州东江威立雅的全方位文件安全管理
  17. 【压缩感知合集5】压缩感知简介和数学模型分析
  18. 对CreateCompatibleDC的粗浅认识
  19. 将opera强制的搜狗转为百度搜索
  20. MATLAB中用fprintf函数实现矩阵原样输出

热门文章

  1. git stash 个人理解
  2. getdata提取曲线数据_Origin如何从图表中获取数据
  3. Leetcode 46.全排列 (每日一题 20210621)
  4. 线性代数笔记:Khatri-Rao积
  5. MCMC笔记:蒙特卡罗方法
  6. LeetCode面试刷题技巧- 贪心算法题习题集
  7. MATLAB table数据结构 再篇
  8. 基于SIFT特征的全景图像拼接
  9. hadoop学习--K-Means(聚类算法)
  10. Matplotlib实例教程(十四)误差条形图