View the sort command instructions for file and content processing.
1. Command description:
The sort command sorts the text content of a file.
2. Syntax:
Sort [Option]... [file list]...
3. instance:
To demonstrate sort usage, create a file first. The file name and content are as follows:
[root@CentOS opt]# cat sort.txt b:3c:2a:4e:5d:1f:11[root@CentOS opt]#
Next, sort the output content:
[Root @ CentOS opt] # cat sort.txt | sort ???? # When the output content is directly sorted, a: 4b: 3c: 2d: 1e: 5f: 11 [root @ CentOS opt] is sorted by the first character of each line by default. #
Next, sort the output content in reverse order:
[Root @ CentOS opt] # cat sort.txt | sort-r ???? #-R option indicates reverse sorting f: 11e: 5d: 1c: 2b: 3a: 4 [root @ CentOS opt] #
The sort.txt file has the following characteristics: the first character is a letter, the third character is a number, and the middle is separated by a colon. In this way, you can use the-t option to specify the delimiter and use the-k option to specify the columns used for sorting:
[root@CentOS opt]# cat sort.txt | sort -t ":" -k 2d:1f:11c:2b:3a:4e:5[root@CentOS opt]#
You may have noticed that the current sorting is performed according to the part of the number column. However, what is 2nd behavior? This is because the current sorting is not performed according to the "number value ". In the preceding command, the current sorting is performed according to the second part separated by colons. By sorting, only the first character is read, and the first character of 11 is 1, sort by characters, which is indeed smaller than 2. If you want to specify the order by number, you need to add the-n option:
[Root @ CentOS opt] # cat sort.txt | sort-t ":"-k 2-n ???? #-N indicates that the values are sorted by numbers d: 1c: 2b: 3a: 4e: 5f: 11 [root @ CentOS opt] #