linux 中的 sort 命令是 gnu coretils 包的一部分。该命令用于按升序 / 降序对指定文件中的文本行或标准输入中的文本行进行排序。排序后的输出可以写入文件或显示在标准输出中。sort 命令是一个简单而强大的具,可用于各种场景,从对文件中的数据进行排序到进一步分析和准备数据。
在本文中,我们将讨论 sort 命令的各种可用选项,并提供一些实际示例来说明其用法。
sort 命令的基本语法如下:
sort [options] [file(s)]
该命令提供了各种选项,可用于自定义排序过程和需要排序的文件。如果没有指定文件,sort 命令将根据标准输入(即键盘)对输入进行排序。
以下是 unix / linux 系统中 sort 命令的一些常用选项:
-r: 将输入按相反顺序排序-n:对输入进行数字排序-k:根据特定字段或列对输入进行排序-b:忽略前面的空格-t:指定字段分隔符-u:从输出中删除重复的行-o:指定输出文件
让我们通过一些示例来仔细研究这些选项。
sorting file content
默认根据第一个字符按升序排序,您可以简单地键入 sort 命令,后跟文件名。
sort data.txt
还可以使用以下命令为该命令提供输入。
sort < data.txt cat data.txt | sort
sorting in reverse order
-r 选项用于以相反的顺序对输入进行排序。
sort -r data.txt
numerical sorting
-n 选项用于对输入进行数字排序,当对文本数字进行排序时这个选项很有用。
sort -n data.txt
sorting by field
-k 选项用于根据特定字段或列对输入进行排序。此选项在对表格数据进行排序时非常有用,其中每一行
表示记录,字段由特定分隔符分隔。字段号是通过选项指定的,字段是从 1 开始编号。
sort -k 2 data.txt
specifying the field separator
-t 选项用于指定按字段排序时的字段分隔符。默认情况下,字段由 空格 或 制表符 分隔。
sort -t "," -k 2 data.txt
removing duplicate lines
-u 选项用于从排序的输出中删除重复的行。当对大文件进行排序时,此选项非常有用。
sort -u data.txt
specifying the output file
-o 选项用于指定输出文件,将排序后的数据保存到另一个文件而不是显示在屏幕上。
sort data.txt -o sorted_data.txt
您还可以使用 > 操作符将内容写入文件。
sort data.txt > sorted_data.txt
combining multiple options
您可以组合不同的选项来排序结果。
例如,将文件按数字倒序排序并保存结果到一个新文件。
sort -nr data.txt -o reverse_sorted_data.txt
当然,你也可以像这样组合不同的选项。
sort -t "," -k 2,3 -u data.txt
方法补充
sort排序规则:
- 以行为单位,每一行作为一个字符串
- 按照字符串的比较规则,首字母开始依次向后按ascii码值进行比较
- 结果默认升序输出
1. 简单排序
[root@linuxforliuhj test]# cat test.txt aaa:cde bbb:dse ccc:123 abc:rew acb:1we 111:fdf 222:esa 333:iud
[root@linuxforliuhj test]# sort test.txt 111:fdf 222:esa 333:iud aaa:cde abc:rew acb:1we bbb:dse ccc:123
使用sort进行简单排序,首字母开始依次进行比较。
2. 排序后去除重复的行,使用参数-u(unique)
[root@linuxforliuhj test]# cat test.txt aaa:cde aaa:cde bbb:dse ccc:123 abc:rew abc:rew abc:rew acb:1we 111:fdf 222:esa 333:iud
[root@linuxforliuhj test]# sort -u test.txt 111:fdf 222:esa 333:iud aaa:cde abc:rew acb:1we bbb:dse ccc:123
排序后将重复的行aaa:cde和abc:rew去重只保留一行。
3.默认排序为升序排序,使用参数-r进行降序排序
[root@linuxforliuhj test]# cat test.txt aaa:cde aaa:cde bbb:dse ccc:123 abc:rew abc:rew abc:rew acb:1we 111:fdf 222:esa 333:iud
[root@linuxforliuhj test]# sort -u -r test.txt ccc:123 bbb:dse acb:1we abc:rew aaa:cde 333:iud 222:esa 111:fdf
进行降序排序,同时去除重复项。
4.将排序结果输出到指定文件,使用参数-o(out)
[root@linuxforliuhj test]# sort -u -r test.txt -o /test/test_1.txt [root@linuxforliuhj test]# cat test_1.txt ccc:123 bbb:dse acb:1we abc:rew aaa:cde 333:iud 222:esa 111:fdf
将排序的结果输出到文件test_1.txt文件中,如果输出文件不存在会自动创建。同时也可以使用重定向符号进行输出。
[root@linuxforliuhj test]# sort -u -r test.txt > /test/test_2.txt [root@linuxforliuhj test]# cat test_2.txt ccc:123 bbb:dse acb:1we abc:rew aaa:cde 333:iud 222:esa 111:fdf
参数-o和重定向符号的唯一区别:
- 当使用重定向符号将排序结果重定向输出回源文件进行覆盖时,会出错,源文件会被清空。
- 当使用-o参数时,则能够正常覆盖回源文件。
[root@linuxforliuhj test]# sort -u -r test.txt > /test/test.txt [root@linuxforliuhj test]# ll -d /test/test.txt -rw-r--r--. 1 root root 0 oct 25 20:06 /test/test.txt [root@linuxforliuhj test]# cat test.txt [root@linuxforliuhj test]#
[root@linuxforliuhj test]# sort -u -r test.txt -o /test/test.txt [root@linuxforliuhj test]# cat test.txt ccc:123 bbb:dse acb:1we abc:rew aaa:cde 333:iud 222:esa 111:fdf [root@linuxforliuhj test]#
5.以数值进行比较,而并非字符串,使用参数-n(number)
[root@linuxforliuhj test]# cat test_n.txt 12322 54 735 9 [root@linuxforliuhj test]#
[root@linuxforliuhj test]# sort test_n.txt 12322 54 735 9
[root@linuxforliuhj test]# sort -n test_n.txt 9 54 735 12322
未使用-n参数则从首字母开始依次进行排序;使用-n参数后则按照数值进行排序;此参数只适用于纯数字字符串。
6.将每一行按照指定字符分割以后,选择其中的某一列进行排序,使用-t参数指定分割符,使用-k参数指定按照分割后的哪一列进行排序。
[root@linuxforliuhj test]# cat test.txt asd:34w:dsw ccc:123:343 bbb:123:dfe aab:1we:hiu abc:lwe:htt aaa:cde:lpu 333:esa:qas 222:esa:ase 111:fdf:cxs
以:分割每一行并按照第1列进行排序
[root@linuxforliuhj test]# sort -t ':' -k 1 test.txt 111:fdf:cxs 222:esa:ase 333:esa:qas aaa:cde:lpu aab:1we:hiu abc:lwe:htt asd:34w:dsw bbb:123:dfe ccc:123:343
以:分割每一行并按照第2列进行排序
[root@linuxforliuhj test]# sort -t ':' -k 2 test.txt ccc:123:343 bbb:123:dfe aab:1we:hiu asd:34w:dsw aaa:cde:lpu 222:esa:ase 333:esa:qas 111:fdf:cxs abc:lwe:htt
以:分割每一行并按照第3列进行排序
[root@linuxforliuhj test]# sort -t ':' -k 3 test.txt ccc:123:343 222:esa:ase 111:fdf:cxs bbb:123:dfe asd:34w:dsw aab:1we:hiu abc:lwe:htt aaa:cde:lpu 333:esa:qas
7.sort命令适用于管道符
[root@linuxforliuhj test]# cat test.txt ccccc bdsdsad adsfwe gdgsdf we w dsafswqeqw jhhtyty
[root@linuxforliuhj test]# cat test.txt | sort adsfwe bdsdsad ccccc dsafswqeqw gdgsdf jhhtyty w we
[root@linuxforliuhj test]# cat test.txt | sort -r we w jhhtyty gdgsdf dsafswqeqw ccccc bdsdsad adsfwe [root@linuxforliuhj test]#
8.注意
注意:
【1】sort还有一些其他的不常用的参数,例如-f,-b , -c, -c等
【2】sort默认是使用-b参数的,即默认删除每一行之前的空格,从第一个非空格数据开始比较
[root@linuxforliuhj test]# cat te.txt hello this is hello this is hellothis is hello this a
[root@linuxforliuhj test]# sort te.txt hello this a hello this is hello this is hellothis is
可以看出默认的sort命令忽略每一行第一个字符之前的空格,而并非整行所有的空格!
9.忽略大小写参数-f引发的奇怪现象
[root@linuxforliuhj test]# cat hello.sh a a b b d d e e
[root@linuxforliuhj test]# sort hello.sh a a b b d d e e
[root@linuxforliuhj test]# sort -f hello.sh a a b b d d e e
到此这篇关于linux中使用sort命令排序的完整教学的文章就介绍到这了,更多相关linux sort命令排序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论