sort命令对一个文件(文本文件)的内部数据进行排序,其将输入文件看做由多条记录组成的数据流,而记录由可变宽度的字段组成,以换行符为定界符,其将一条记录分成多个域来处理,默认的域分割符为空格(也可以由用户自己通过sort的参数进行指定)。
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -t: score
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
WangWu:Chinese:60:Math:71:English:100
LiSi:Chinese:80:Math:71:English:85
ZhangSan:Chinese:80:Math:91:English:100
SunQi:Chinese:100:Math:91:English:100
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -r -t: score
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -r -t: score -o scoresortoutput
alloy@raspberrypi:~/linuxshell/chapter5$ cat scoresortoutput
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
sort的标准调用格式说明如下:
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
sort [OPTION]... --files0-from=F
其中OPTION为sort的参数,FILE为sort操作的目标文件,常用的sort参数说明如下:
-k:指定sort排序的域,也就是说对文件的第几项进行排序,使用数字1~N来指定具体的域。
-n:按照数字大小进行排序,通常来说sort的排序会按照字符顺序来进行,即使该域的字符串实际上是个数字,使用-n参数可以将该字符串作为数字来处理。
-r:将排序结果逆序输出。
-t:改变域分隔符,当文件的每个域之间的分割符不是默认的空格的时候该可以使用该命令来指定分割符。
-u:去除文件中重复的项(行)。
-o:将排序结果输出到一个指定文件。
使用vim建立一个如下的文件score,这是一个用于记录学生成绩的文件,第一项是姓名,后面分别是语文、数学和英语成绩。
ZhangSan:Chinese:80:Math:91:English:100
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
SunQi:Chinese:100:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
SunQi:Chinese:100:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
直接使用sort对文件进行排序,其默认会对域1也即学生姓名进行排序,将学生姓名的首字母进行排序:
alloy@raspberrypi:~/linuxshell/chapter5$ sort score
LiSi:Chinese:80:Math:71:English:85
SunQi:Chinese:100:Math:91:English:100
WangWu:Chinese:60:Math:71:English:100
ZhangSan:Chinese:80:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
LiSi:Chinese:80:Math:71:English:85
SunQi:Chinese:100:Math:91:English:100
WangWu:Chinese:60:Math:71:English:100
ZhangSan:Chinese:80:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
由于文件的分隔符是;而不是空格,所以如果对后续的域进行排序则必须使用分割符指定参数-t,此时对语文成绩进行排序操作,使用-k + 数字来指定域
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -t: score
SunQi:Chinese:100:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
WangWu:Chinese:60:Math:71:English:100
LiSi:Chinese:80:Math:71:English:85
ZhangSan:Chinese:80:Math:91:English:100
SunQi:Chinese:100:Math:91:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
WangWu:Chinese:60:Math:71:English:100
LiSi:Chinese:80:Math:71:English:85
ZhangSan:Chinese:80:Math:91:English:100
可以看到此时语文成绩的排序结果是按照字母顺序进行的,如果要将其当做数字进行操作,还需要使用参数-n。
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -t: score
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
WangWu:Chinese:60:Math:71:English:100
LiSi:Chinese:80:Math:71:English:85
ZhangSan:Chinese:80:Math:91:English:100
SunQi:Chinese:100:Math:91:English:100
此时语文成绩的排序是从小到大的,可以使用-r来逆向变成从大到小
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -r -t: score
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74
可以看到文件中出现了重复条目,可以使用-u参数将其中重复的内容删除,需要注意的-u命令删除的是-k参数指定的域,也即是目前指定的是语文成绩,如果想要删除学生姓名ZhaoLiu,应该对-k1进行-u操作
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -r -u -t: score
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
可以用-o参数将排序结果输出到一个文件
alloy@raspberrypi:~/linuxshell/chapter5$ sort -k3 -n -r -t: score -o scoresortoutput
alloy@raspberrypi:~/linuxshell/chapter5$ cat scoresortoutput
SunQi:Chinese:100:Math:91:English:100
ZhangSan:Chinese:80:Math:91:English:100
LiSi:Chinese:80:Math:71:English:85
WangWu:Chinese:60:Math:71:English:100
ZhaoLiu:Chinese:56:Math:41:English:74
ZhaoLiu:Chinese:56:Math:41:English:74