Linux sort command usage

  • 2020-04-02 01:48:50
  • OfStack

Sort is to help us sort according to different data types, its syntax and common parameter format:
Sort [-bcfmnrtk][source file][-o output file]
Note: sort can be sorted by behavior unit for the content of a text file.

Parameters:
  - b     Ignore the space character at the beginning of each line.
  - c     Check that the files are sorted in order.
  The -f     When sorting, ignore uppercase and lowercase letters.
  The -m     Sort the first three letters by the abbreviations of the month.
  - n     Sort by value.
  -o < The output file >     Stores the sorted results in the specified file.
  - r     Sort in reverse order.
  -t < Separator characters >     Specifies the column separator character to be used when sorting.
  - k.   Choose which interval to sort by.
  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Here are a few examples of using Sort.

(1) sort takes each line of a file as a unit and compares them with each other, starting with the first character, going back to the ASCII value, and then printing them in ascending order.

[rocrocket @ rocrocket programming] $cat seq. TXT
banana
apple
The pear
orange
[rocrocket @ rocrocket programming] $sort seq. TXT
apple
banana
orange
The pear

Users can save the sorted file contents or output the sorted file contents to the printer. In the following example, the user saves the sorted file contents to a file named result.
$Sort seq. TXT > The result

(2) the -u option of sort

It simply removes duplicate lines from the output line.

[rocrocket @ rocrocket programming] $cat seq. TXT
banana
apple
The pear
orange
The pear
[rocrocket @ rocrocket programming] $sort seq. TXT
apple
banana
orange
The pear
The pear
[rocrocket@rocrocket programming]$sort-u seq.txt
apple
banana
orange
The pear

Pear was ruthlessly removed by the -u option due to duplication.

(3) the -r option of sort

Sort by default is sort in ascending order, if you want to sort in descending order, just add -r and you're done.

[rocrocket @ rocrocket programming] $cat number. TXT
1
3
5
2
4
[rocrocket @ rocrocket programming] $sort number. TXT
1
2
3
4
5
[rocrocket@rocrocket programming]$sort-r number.txt
5
4
3
2
1
(5) the -o option of sort

Because sort outputs the result to standard output by default, a redirect is needed to write the result to a file, like sort filename > Newfile.

However, if you want to output the sorted results to the original file, redirection is not an option.

[rocrocket@rocrocket programming]$sort-r number.txt > Number. TXT
[rocrocket @ rocrocket programming] $cat number. TXT
[rocrocket @ rocrocket programming] $
Look, I've cleared the number.

At this point, the -o option appears and successfully solves the problem, allowing you to safely write the result to the original file. This may be the only advantage of -o specific gravity orientation.

[rocrocket @ rocrocket programming] $cat number. TXT
1
3
5
2
4
[rocrocket@rocrocket programming]$sort-r number.txt-o number.txt
[rocrocket @ rocrocket programming] $cat number. TXT
5
4
3
2
1

(6) the -n option of sort

Have you ever had a situation where 10 is less than 2? I've been there. This happens because the sorting program sorts the Numbers by characters, and the sorting program compares the Numbers 1 and 2 first, so obviously 1 is smaller, so put 10 before 2. This is sort's style.

If we want to change this, we use the -n option to tell sort, "sort by number"!

[rocrocket @ rocrocket programming] $cat number. TXT
1
10
19
11
2
5
[rocrocket @ rocrocket programming] $sort number. TXT
1
10
11
19
2
5
[rocrocket@rocrocket programming]$sort-n number.txt
1
2
5
10
11
19

(7) the -t and -k options of sort

If the contents of a file are as follows:

[rocrocket @ rocrocket programming] $cat facebook. TXT
Banana: cannot profit. 5
Apple: then. 5
The pear: 90:2.3
Orange: David. 4

The file has three columns, separated by a colon, the first column for the type of fruit, the second column for the quantity of fruit, and the third column for the price of fruit. So I want to sort by the number of fruits, by the second column, how do I use sort implementation? Fortunately, sort provides the -t option, which can be followed by a spacer. After specifying the spacer, you can specify the number of columns with -k.

[rocrocket @rocrocket programming] $sort-n-k 2-t ':' facebook.txt
Apple: then. 5
Orange: David. 4
Banana: cannot profit. 5
The pear: 90:2.3

(8) other sort common options

-f converts all lowercase letters to uppercase for comparison, meaning case is ignored

-c checks to see if the file is in order, and if it is out of order, prints information about the first out-of-order line and returns 1

-c checks to see if the file is in order, and if it is out of order, no content is printed and only 1 is returned

-m will sort by month, so JAN is less than FEB and so on

-b will ignore all the white space before each line, starting with the first visible character.


Related articles: