Linux command | command sort

sort is a frequently used command in Linux commands, used to sort text content, the following will be introduced with examples.

1. Basic concepts

sort sorts the input content. The input content can be a file or standard input. The content of the original file is not modified by default.

2. Syntax and parameters

sort [OPTION]... [FILE]...

Common parameters:

-t Separator: Specify the separator used when sorting;

-k: Specify the column to be sorted;

-n: sort according to numerical value;

-r: sort in reverse order;

-b:  Ignore the leading spaces in the sort field or keywords;

-c: Check whether the content to be sorted is sorted, if it is not sorted, output the first out-of-order column, otherwise, there is no output;

-o FILE: output the sorting result to the specified file instead of standard output;

Three, examples

The sorting text content used in the example is the first 10 lines of /etc/passwd after processing, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]#

3.0 default

By default, sort sorts according to ASCII code, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]#
[root@localhost ~]# sort passwd-sort
adfm:x:3:4
bion:x:1:1
daen:x:2:2
halt:x:7:5
laap:x:4:7
mail:x:8:12
oper:x:11:21
root:x:0:0
shut:x:6:3
sync:x:5:9
[root@localhost ~]#

3.1 -t and -k parameters

Separate by " : ", select the third column for sorting, as shown below:

[root@localhost ~]# sort -t ":" -k 3 passwd-sort
root:x:0:0
bion:x:1:1
oper:x:11:21
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
[root@localhost ~]#

Note: The ASCII code is used for sorting by default, as can be seen from the arrangement of the third line.

3.2 -n parameter

Separate by " : ", select the third column to sort by numerical value, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]# sort -t ":" -k 3 -n passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]#

The sorting result shows the numerical value in the third column separated by ":" from small to large. 

3.3 -r parameter

Separate by " : ", select the third column to sort in the reverse order by default (ie: ACSII code) , as shown below:

[root@localhost ~]# sort -t ":" -k 3 -r passwd-sort
mail:x:8:12
halt:x:7:5
shut:x:6:3
sync:x:5:9
laap:x:4:7
adfm:x:3:4
daen:x:2:2
oper:x:11:21
bion:x:1:1
root:x:0:0
[root@localhost ~]#

Note the line where "oper:x:11:21" is located.

Separate  by " : ", and select the third column to sort in reverse order by value , as shown below:

[root@localhost ~]# sort -t ":" -k 3 -r -n passwd-sort
oper:x:11:21
mail:x:8:12
halt:x:7:5
shut:x:6:3
sync:x:5:9
laap:x:4:7
adfm:x:3:4
daen:x:2:2
bion:x:1:1
root:x:0:0
[root@localhost ~]#

After sorting, the values ​​in the third column separated by ":" are sorted from largest to smallest. 

3.3 -c parameter

Determine whether the input content has been sorted, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]# sort -c passwd-sort
sort:passwd-sort:2:无序: bion:x:1:1
[root@localhost ~]# sort -t ":" -k 3 -n -c passwd-sort
[root@localhost ~]#

Begin to check according to the default order, showing that the second row is out of order. Then, use " : " as the separator, and select the third row to check whether it is sorted or not. There is no output, which means the sorting is done. 

3.4 -o parameter

Output the sort result of sort to the specified file, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]# sort -t ":" -k 3 passwd-sort -o passwd-sort-file
[root@localhost ~]# cat passwd-sort-file
root:x:0:0
bion:x:1:1
oper:x:11:21
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
[root@localhost ~]#

You can also use ">" to output the sorting results of sort to a specified file, as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]# sort -t ":" -k 3 passwd-sort > passwd-sort-file1
[root@localhost ~]# cat passwd-sort-file1
root:x:0:0
bion:x:1:1
oper:x:11:21
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]#

After sort sorts the output, the content of the passwd-sort file remains unchanged, and the content of the passwd-sort-file1 file is the sorted content.

Note: You cannot use ">" to output the sort result to the original file , as shown below:

[root@localhost ~]# cat passwd-sort
root:x:0:0
bion:x:1:1
daen:x:2:2
adfm:x:3:4
laap:x:4:7
sync:x:5:9
shut:x:6:3
halt:x:7:5
mail:x:8:12
oper:x:11:21
[root@localhost ~]# sort -t ":" -k 3 passwd-sort > passwd-sort
[root@localhost ~]# cat passwd-sort
[root@localhost ~]#

 After redirecting to the original file through the ">" operator, the content of the original file is empty.

Four, summary

Sort uses ASCII code for sorting by default. If you need to add the parameter -n to sort by value, the parameters that are often used are -t and -k. If output redirection is not used, the content of the original file is not modified by default.

Five, references

[1] https://man7.org/linux/man-pages/man1/sort.1.html

 

Guess you like

Origin blog.csdn.net/u011074149/article/details/112387674