Linux sort command sort
The sort command is a commonly used sorting command in Linux , and it is also a pipeline command .
In order to ensure that future instances can get the sorting results we want, we need to set the following
# export LC_ALL=C
Okay, next we will officially start using sort.
# sort [options]…[FILE]
-c checks whether the file contents are in order. If they are out of order, outputs the position information where the out of order begins.
# cat /tmp/sort.txt
a
b
c
d
e
f
k
g
# sort –c /tmp/sort.txt
sort: /tmp/sort.txt:8: disorder: g
We can see that the disorder information shows that the disorder starts at g in line 8 in the /tmp/sort.txt file.
-b ignores leading spaces when sorting. By default, spaces are also sorted.
# cat /tmp/sort.txt
cat
delete
alpha
glue
error
recruise
Error
key
hello
# sort /tmp/sort.txt
key
Error
alpha
cat
delete
error
glue
hello
recruise
We can see that the spaces before the key are also sorted and compared.
# sort –b /tmp/sort.txt //Use the -b option to ignore leading spaces
Error
alpha
cat
delete
error
glue
hello
key
recruise
-f Ignore case and convert all lowercase letters to uppercase for comparison.
# sort –bf /tmp/sort.txt
alpha
cat
delete
Error
error
glue
hello
key
recruise
-u remove duplicates
# sort /tmp/sort.txt
alpha
cat
error
error
hello
recruise
//We can see that there are two duplicates in the result error
# sort –u /tmp/sort.txt //Use -u to remove duplicates
alpha
cat
error
hello
recruise
-n uses pure numbers to sort. For numbers, by default, the numbers on each digit are compared and sorted one by one. Using this option will sort by value.
# cat /tmp/sort.txt
10
2
45
358
21
3
8
# sort /tmp/sort.txt //Default sorting
10
2
21
3
358
45
8
We can see that 10 comes before 2, while 8 comes last.
# sort –n /tmp/sort.txt //Use the -n option
2
3
8
10
21
45
358
After using -n, the entire content is sorted by value.
-r sorts the results in reverse order
# sort –n -r /tmp/sort.txt
358
45
21
10
8
3
2
-o 将排序结果输出到文件中
# sort –n /tmp/sort.txt –o /tmp/sort1.txt
# cat /tmp/sort1.txt
2
3
8
10
21
45
358
当然了,这个我们也可以通过重定向 > 来实现。
# sort –n /tmp/sort.txt > /tmp/sort1.txt
但是,重定向不能用于将数据重定向到原文件
# sort –n /tmp/sort.txt > /tmp/sort.txt
# cat /tmp/sort.txt
//内容为空
# sort –n /tmp/sort.txt –o /tmp/sort.txt
这样 /tmp/sort.txt文件中的内容就是已经排好序的数据了。
哎呀!上面说了这么多了,是不是有点啰嗦了。Sort命令的选项很多,我们就不一一举例了。下面我们再介绍两个选项 –t 和 –k。剩下的我们只介绍其含义。
-t 分隔符,将每一行数据按照该选项指定的分隔符分成多个域。默认情况下是使用tab键或者空格符来分割的。
-k 指定用哪个域的数据来进行排序
-t和-k一般情况下是同时出现的。如果只使用-t的话,系统会默认使用分割以后的第一个域内的数据进行排序
# cat /tmp/sort.txt
onmpw:web:10:jy
onmpw:site:5:www
jiyi:web:8:www
domain:site:10:alph
child:node:5:js
# sort –t ‘:’ /tmp/sort.txt
child:node:5:js
domain:site:10:alph
jiyi:web:8:www
onmpw:site:5:www
onmpw:web:10:jy
很明显是根据第一段的来进行排序的。
# sort –t ‘:’ –k 2 /tmp/sort.txt
child:node:5:js
domain:site:10:alph
onmpw:site:5:www
onmpw:web:10:jy
jiyi:web:8:www
这里着重介绍一下-k选项
-k field[.start[,field.end]][标识][ -k field[.start[,field.end]][标识]]…
通常情况下-k配合-t一起使用,如果单独使用-k的话,那默认的分割符是tab键或者空格符。-k后面指定由哪个域来进行匹配,在一条命令中可以使用多个-k。当第一个-k选项指定的域有重复项的时候再根据第二个-k指定的域,如果还有重复项的话再根据第三个-k指定的域,依次类推得到最后的结果。默认情况下,如果只有一个-k选项,当有重复项的时候会根据第一个域的字段进行匹配。
field就是指定的第几个域;.start表示从该域的字段的第start个字符开始匹配,默认是1;field.end表示到第end个字符截止。也就是说匹配该域字段的start和end之间的字符。end可以省略,如果省略的话表示到该域的末尾。
# cat /tmp/sort.txt
onmpw:web:10:jy
onmpw:site:5:www
jiyi:web:8:www
domain:site:10:alph
child:node:5:js
# sort –t : -k 2.1 /tmp/sort.txt //从第二个字段的第一个字符开始直到第二个字段的结尾
child:node:5:js
domain:site:10:alph
onmpw:site:5:www
onmpw:web:10:jy
jiyi:web:8:www
# sort –t : -k 2.2,2.3 /tmp/sort.txt //从第二个字段的第二个字符开始直到第三个字符
jiyi:web:8:www
onmpw:web:10:jy
domain:site:10:alph
onmpw:site:5:www
child:node:5:js
我们看上面的结果是不是有较大的出入。再看当第二个字段有相同值的默认情况下是按照第一个域的字段进行匹配的,我们看site那两行数据,再次按照第一个字段的话domain会在onmpw前面。这个不是我们想要的结果,我们想要第二个域的字段相等的时候按照第三个域的字段进行匹配。那就需要用到第二个-k选项了。
# sort –t : -k 2.2,2.3 –k 3 /tmp/sort.txt
onmpw:web:10:jy
jiyi:web:8:www
domain:site:10:alph
onmpw:site:5:www
child:node:5:js
好像和上面的结果没有什么区别哦!没错,结果是一样。但是其排序已经是按照第三个域进行排序了,只是默认情况下不是按照数值排序的,而是也将数字的每一位按照字符排序,所以10排在了5前面。
# sort –t : -k 2.2,2.3 –k 3,3n /tmp/sort.txt
jiyi:web:8:www
onmpw:web:10:jy
onmpw:site:5:www
domain:site:10:alph
child:node:5:js
Now let’s see if the result is correct. The above -k 3,3n is equivalent to –k 3n
# sort –t : -k 2.2,2.3 –k 3n /tmp/sort.txt
We can see that 3 is followed by an n. This n is our flag, which is equivalent to the meaning of –n. The flag here is only valid for the current group. What does it mean? In the example above, we can see that there are two groups of identical data sorted according to the second field, while -k 3n uses the data in each group as the data source for sorting.
This may be a bit vague. The result of using -n is not very clear. Now let's use -r as an example. -r means to reverse the sorting order. We replace -k 3n in the above example with -k 3nr.
# sort –t : -k 2.2,2.3 –k 3nr /tmp/sort.txt
onmpw:web:10:jy
jiyi:web:8:www
domain:site:10:alph
onmpw:site:5:www
child:node:5:js
Let's see if the result is different again, because we sorted the data in each group by the third field (-k 3n), and then reversed it by the r flag, so the result is the above result. How can we see that it is only valid within the group? See the following example
# sort –t : -k 2.2,2.3 –k 3n /tmp/sort.txt
jiyi:web:8:www
onmpw:web:10:jy
onmpw:site:5:www
domain:site:10:alph
child:node:5:js
//Normal sort, then we use -r to reverse the order
# sort –t : -r –k 2.2,2.3 –k 3n /tmp/sort.txt
child:node:5:js
onmpw:site:5:www
domain:site:10:alph
jiyi:web:8:www
onmpw:web:10:jy
From the above results we can see that not only the overall sorting is reversed, but also the order within each group is reversed.
This is all we have to say about sort. The above are the commonly used options. You can view the remaining options yourself using the command 'info sort'. I hope this article is helpful to you.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Sort a collection by date in MongoDB
Publish Date:2025/04/11 Views:64 Category:MongoDB
-
In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Sort by RAND in MySQL
Publish Date:2025/04/09 Views:176 Category:MySQL
-
Summary: in this tutorial, we will learn how to shuffle or sort the values or records of a table in MySQL. Most businesses and organizations that use MySQL for data analysis or visualization need to sort different table values o
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –