Linux Chinese text processing tool sort command details

  • 2020-05-30 21:44:01
  • OfStack

preface

sort command will compare every 1 row as a unit, compare the principle from the first character, back in turn by ASCII code value comparison, finally put them in the order of 1 set output, in fact, sort command can be considered to be a very powerful tool for data governance, governance content similar to the database record file. Let's take a look.

1. sort actual combat training

sort.txt


root,x,0,0,root,/root,/bin/bash
daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
bin,x,2,2,bin,/bin,/usr/sbin/nologin
bin,x,2,2,bin,/bin,/usr/sbin/nologin
sys,x,3,3,sys,/dev,/usr/sbin/nologin

number.txt


 9
8
90
82
2

human_readable.txt


2k
2G
2M
20M

We practice

Output the ascending sort file


> root@wing:~/wing # sort sort.txt 
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
> daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
> root,x,0,0,root,/root,/bin/bash
> sys,x,3,3,sys,/dev,/usr/sbin/nologin
>
>

Output a descending sort file


> root@wing:~/wing # sort -r sort.txt 
> sys,x,3,3,sys,/dev,/usr/sbin/nologin
> root,x,0,0,root,/root,/bin/bash
> daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
>
>

Output the sort file in ascending order in column 3


> root@wing:~/wing # sort -t , -k 3 sort.txt 
> root,x,0,0,root,/root,/bin/bash
> daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
> sys,x,3,3,sys,/dev,/usr/sbin/nologin
>
>

Remove the descending output of duplicate rows


> root@wing:~/wing # sort -ur sort.txt 
> sys,x,3,3,sys,/dev,/usr/sbin/nologin
> root,x,0,0,root,/root,/bin/bash
> daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
> bin,x,2,2,bin,/bin,/usr/sbin/nologin
>

2. Common sort parameters

- b � ignore leading -- blanks

Ignore the space at the beginning of each line and start with the first character that is not a space.

- c � check, � check = diagnose - first

Check if the file is sorted, output the first unsorted line if it is not, and return 1 if it is.


>root@wing:~/wing # sort -c sort.txt 
>sort: sort.txt:2: disorder: daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
>

- C � check = quiet, � check = silent

Unlike the -c parameter function 1, this parameter does not output the first unsorted row if it is not sorted, and 2 outputs nothing.


> root@wing:~/wing # sort -c sort.txt 
> sort: sort.txt:2: disorder: daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
> root@wing:~/wing # 
>

- f � ignore - case

Ignore case and convert all lowercase letters to uppercase letters for comparison.

- h � human numeric -- sort

Sort in a human-readable way, such as k,G.


> root@wing:~/wing # sort human_readable.txt 
> 20M
> 2G
> 2k
> 2M
> root@wing:~/wing # sort -h human_readable.txt 
> 2k
> 2M
> 20M
> 2G
>

- M � month - sort

Sort by month, JAN,MAR, and so on.

- n � numeric - sort

Sort Numbers into numeric values.


 9
8
90
82
2
0

- R � random - sort

Sort in a random way.


 9
8
90
82
2
1

- r � reverse

The default is ascending sort, plus the -r parameter is descending sort.

- o � output = FILE

Output the results of the sort command to another file.

- u � unique

And the -c parameter 1, it's not useful;

When not associated with the -c parameter 1, all rows are deduplicated and sorted out.

- t � field - separator

The separator.

- k � key

Sort by key, which can be the position or type of the column.

conclusion


Related articles: