Many times the need for Linux text relatively standard text sorting data, this time you can use the sort command in Linux systems for processing.
Syntax: sort [-ntkr] filename
Common parameters:
-n sorted according to figures
-t specified delimiter
-k specified delimiter of columns
-r descending order
Example usage:
Sort.txt create a text file that represents the subjects of performance, as follows:
a 98 80 100 95
d 88 99 95 82
b 89 83 80 100
c 60 98 79 90
cat sort.txt | sort # default will be sorted by the first column
Output:
a 98 80 100 95
b 89 83 80 100
c 60 98 79 90
d 88 99 95 82
cat sort.txt | sort -n -k 2 # sorted by the second column
Output:
c 60 98 79 90
d 88 99 95 82
b 89 83 80 100
a 98 80 100 95
cat sort.txt | sort -n -t " " -k 3 -r
# According to a third column, designated delimiter is a space, in descending order
d 88 99 95 82
c 60 98 79 90
b 89 83 80 100
a 98 80 100 95
Note: If you do not add -n parameters, Linux default sort column will be sorted by the first character, such as 213 if not -n parameters, the system will first 21 characters 2 and 3 and in accordance with the comparison, if the ascending 21 will be in front of 3, if all the numbers are sort -n parameter must be added to ensure the correctness of the sort.
Reproduced in: https: //www.jianshu.com/p/5d5026002d3c