Centos command -- sort

Source: Internet
Author: User

Function Description: sorts text files.
Syntax: sort [-bcdfimMnr] [-o <output file>] [-t <delimiter>] [+ <start column>-<End column>] [-- help] [-- verison] [file]
Note: sort can sort the content of text files by unit of action.
Parameters:
-B ignores the leading space characters in each line.
-C: Check whether the files are sorted in order.
-D: When sorting, other characters are ignored except English letters, numbers, and spaces.
-F: lowercase letters are considered as uppercase letters.
-In I sorting, except for ASCII characters between 176 and, other characters are ignored.
-M combines several sorted files.
-M sorts the first three letters by the abbreviation of the month.
-N is sorted by the value size.
-O <output file> stores the sorted results to the specified file.
-R is sorted in reverse order.
-T <delimiter> specifies the column delimiter used for sorting.
+ <Start column>-<End column> is sorted by the specified column, and the range is from the start column to the first column of the end column.
-- Help: displays help.
-- Version: displays version information.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following describes how to use Sort through several examples.

Use the Sort command to Sort all rows in the text file and output the result. Note that the first word on the second and third lines of the original file is exactly the same. This command will continue to compare the second word vegetables with the first character of fruit.

$ Cat text
Vegetable soup
Fresh vegetables
Fresh fruit
Lowfat milk

$ Sort text
Fresh fruit
Fresh vegetables
Lowfat milk
Vegetable soup

You can save the sorted file content or output the sorted file content to the printer. In the following example, the user saves the sorted file content to the file named result.

$ Sort text> result

Sort the content of the file example using 2nd fields as the sorting keyword.

$ Sort + 1-2 example

For file1 and file2 files, the results are placed in outfile, and the first character of the 2nd fields is used as the sorting keyword.

$ Sort-r-o outfile + 1.0-1.1 example

Sort sorting is often used in pipelines with other commands to combine complex functions, such as sending files in the current working directory to Sort for sorting, the sorting keywords are 6th to 8th fields.

$ Ls-l | Sort + 5-7
$ Ps-e-o "comm pid time" | Sort-d // Sort by the alphabetic order of the first letter of the command

The Sort command can also operate on standard input. For example, if you want to merge several file text lines and sort the merged text lines, you can first use the command cat to merge multiple files, then, use the pipeline operation to input the merged text lines to the command Sort. The Sort command will output the merged and sorted text lines. In the following example, the text lines of the file veglist and file fruitlist are merged and sorted and saved to the file clist.

$ Cat veglist fruitlist | Sort> clist
Sort + 3-4 All-Unigene_PlantTFFamily_Abstract.out> All-Unigene_PlantTFFamily_Abstract.sort
Sort-k 4 All-Unigene_PlantTFFamily_Abstract.out> All-Unigene_PlantTFFamily_Abstract.sort1

Sort two columns of a file, one column in positive order and one column in reverse order.

Sort-k1, 1n-k2, 2nr file

One column in reverse order and two columns in positive order

Sort-k1, 1nr-k2, 2n tes.txt> result.txt

Sort two columns of a file

Sort-k1, 1n-k2, 2n file

1. How sort works

Sort compares each row of a file as a unit. The comparison principle is to compare the lines from the first character to the back, compare them by ASCII code values, and output them in ascending order.
[Rocrocket @ rocrocket programming] $ cat seq.txt
Banana
Apple
Pear
Orange
[Rocrocket @ rocrocket programming] $ sort seq.txt
Apple
Banana
Orange
Pear

2 sort-u Option

The function is very simple, that is, to remove duplicate rows in the output row.

[Rocrocket @ rocrocket programming] $ cat seq.txt
Banana
Apple
Pear
Orange
Pear
[Rocrocket @ rocrocket programming] $ sort seq.txt
Apple
Banana
Orange
Pear
Pear
[Rocrocket @ rocrocket programming] $ sort-u seq.txt
Apple
Banana
Orange
Pear

Pear is repeatedly deleted by the-u option.

3 sort-r Option

The default sorting method of sort is ascending. If you want to change it to descending order, add-r.

[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
4 sort-o options

Because sort outputs the result to the standard output by default, you need to use redirection to write the result to a file, such as sort filename> newfile.

However, if you want to output the sorting result to the original file, you cannot use redirection.

[Rocrocket @ rocrocket programming] $ sort-r number.txt> number.txt
[Rocrocket @ rocrocket programming] $ cat number.txt
[Rocrocket @ rocrocket programming] $
Check that the number is cleared.

At this point, the-o option appears. It successfully solves this problem and allows you to write the result to the original file with confidence. This is perhaps the only advantage of-o proportional targeting.

[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

5 sort-n Option

Have you ever encountered 10 to 2 problems. I have encountered it. This is because the sorting program sorts these numbers by characters. The sorting program will first compare 1 and 2, obviously 1 is small, so it will put 10 in front of 2. This is also the consistent style of sort.

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

[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

6 sort-t option and-k Option

If the content of a file is as follows:

[Rocrocket @ rocrocket 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 colons. The first column indicates the fruit type, the second column indicates the fruit quantity, and the third column indicates the fruit price.

So I want to sort by the number of fruits, that is, by the second column. How can I use sort to achieve this?

Fortunately, sort provides the-t option, and you can set the delimiter later. (Do you think of the-d option of cut and paste, and resonate ~~)

After the Delimiter is specified, you can use-k to specify the number of columns.

[Rocrocket @ rocrocket programming] $ sort-n-k 2-t: facebook.txt
Apple: 10: 2.5
Orange: 20: 3.4
Banana: 30: 5.5
Pear: 90:2.3

We use the colon as the delimiter and sort the values in ascending order for the second column. The result is satisfactory.

7. Other common sort options

-F converts lowercase letters to uppercase letters for comparison, that is, the case is ignored.

-C checks whether the file is sorted out. If the file is in disordered order, information about the first unordered row is output, and 1 is returned.

-C checks whether the file is sorted out. If the file is not output in disordered order, only 1 is returned.

-M is sorted by month, for example, JAN is smaller than FEB.

-B ignores all the blank parts in front of each line and compares them from the first visible character.

Related Article

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.