Linux Sort Usage

Source: Internet
Author: User

Sort The command is to help us sort according to different data types, its syntax and common parameter formats:
  sort [-bcfmnrtk][ source file ][-o output file ]
Additional notes: Sort can be ordered for the contents of a text file in the unit of behavior.

Parameters:
?? -B???ignores the space characters that begin before each line.
?? -C???Check that the files are sorted in order.
?? -F???when sorting, ignores uppercase and lowercase letters.
?? -M???the front3the letters are sorted according to the abbreviation of the month.
?? -N???sorts according to the size of the numbers.
?? -o<Output File>???The sorted result is stored in the specified file.
?? -R???sorted in reverse order.
?? -t<Delimited characters>???Specifies the field separator character to use when sorting.
? K-??Choose which interval to sort by.
?? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Below are a few examples to tell sort

( 1 ) sort each line of the file as a unit, compared to each other, the principle of comparison is from the first character backward, press ascii code values are compared and finally they are output in ascending order.

[[email protected] programming] # Cat Seq.txt
Banana
Apple
Pear
Orange
[[email protected] programming] # Sort Seq.txt
Apple
Banana
Orange
Pear

The user 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 > result

( 2 ) Sort of the - u Options

It is simple to remove duplicate rows in the output line.

[[email  protected] Programming]# cat seq.txt
Banana
Apple
Pear
Orange
Pear
[[email protected] Programming]# sort seq.txt
apple
Banana
Orange
Pear
Pear
[[email protected] Programming]# sort-u seq.txt
Apple
Banana
Orange
Pear

Pear because the repetition is - u the options are ruthlessly removed.

( 3 ) Sort of the - R Options

sort The default sort is ascending, if you want to change to descending, add a -r Span style= "font-family: Song Body" > it's done.

[[email  protected] programming]# cat number.txt
1
3
5
2
4
[[email protected] programming]# Sort Number.txt
1
2
3
4
5
[[email protected] programming]# sort-r number.txt
5
4
3
2
1
( 5 ) sort Span style= "Font-family:verdana" >-o options

because Sort The default is to output the results to standard output, so a redirect is required to write the results to the file, as sort filename > NewFile .

However, if you want to output the sorting results to the original file, redirection is not possible.

[Email protected] programming]# sort-r number.txt > Number.txt
[email protected] programming]# cat Number.txt
[Email protected] programming]#
See, actually will Number emptied out.

just at this time, - o The option appears, which successfully solves this problem, allowing you to confidently write the results to the original file. This may also be the only advantage of the-o specific direction.

[email protected] programming]# cat Number.txt
1
3
5
2
4
[Email protected] programming]# sort-r number.txt-o number.txt
[email protected] programming]# cat Number.txt
5
4
3
2
1

( 6 ) ? Sort of the - N Options

Have you ever encountered a 10 than 2 small situation. I've met anyway. This occurs because the sequencer sorts the numbers by character, and the sequencer compares the 1 2 1 10 placed in 2 front. This is also the usual style of sort

if we want to change this situation, we need to use - N option, to tell Sort , " To sort by numeric values " !

[email protected] programming]# cat Number.txt
1
10
19
11
2
5
[Email protected] programming]# sort Number.txt
1
10
11
19
2
5
[Email protected] programming]# sort-n number.txt
1
2
5
10
11
19

( 7 ) ? Sort of the - T options, and - k Options

If there is a file with the contents of this:

[email protected] programming]# cat Facebook.txt
banana:30:5.5
apple:10:2.5
pear:90:2.3
orange:20:3.4

This file has three columns, separated by a colon between the column and column, the first column indicates the fruit type, the second column represents the fruit quantity, and the third column represents the fruit price. So I would like to sort by the number of fruits, that is, in the second column, how to use the sort implementation? Fortunately,sort provides the - t option, after which you can set the spacer. After you specify a spacer, you can use - k to specify the number of columns.

[Email protected] programming]# sort-n-K 2-t ': ' Facebook.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3

( 8 ) ? Other of Sort Common Options

- F lowercase letters are converted to uppercase letters for comparison, i.e. ignoring case

- C checks to see if the file is ordered and, if so, outputs information about the first row that is out of order, and finally returns 1

- C checks whether the file is sequenced, if it is not in order, does not output content, returns only 1

- M will be sorted by month, such as JAN less than FEB wait

- b ignores all the blank parts preceding each line, starting with the first visible character.

Linux Sort Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.