首页 > 开发 > linux > 正文

Linux-sort排序

2016-10-17 21:32:40  来源:极客头条
概述

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 Usage: sort [OPTION]... [FILE]...  or:  sort [OPTION]... Write sorted concatenation of all FILE(s) to standard output.Mandatory arguments to long options are mandatory for short options too.Ordering options:  -b,   -d,   -f,   -g,   -i,   -M,   -h,   -n,   -R,         -r,                                       general-numeric -g, human-numeric -h, month -M,                                numeric -n, random -R, version -V  -V, Other options:                                  for more use temp files  -c,   -C,                                     decompress them with PROG -d                                  NUL-terminated names in file F;                            If F is - then read names from standard input  -k,                             (default end of line)  -m,   -o,   -s,   -S,   -t,   -T,                               multiple options specify multiple directories  -u,                               without -c, output only the first of an equal run  -z,             POS is F[.C][OPTS], where F is the field number and C the character positionin the field; both are origin 1.  If neither -t nor -b is in effect, charactersin a field are counted from the beginning of the preceding whitespace.  OPTS isone or more single-letter ordering options, which override global orderingoptions 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 usesnative byte values.Report sort bugs to bug-coreutils@gnu.orgGNU coreutils home page: General help using GNU software: For complete documentation, run: info coreutils 'sort invocation'
栗子sort将文件/文本的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。
[root@entel2 ~]# cat st.txt aa:10:1.1 ccc:30:3.3ddd: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.3ddd:40:4.4 eee:50:5.5eee: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”