Linux Sort command parameters and usage detailed

Source: Internet
Author: User
Tags sort sorts

Function Description: Sort the contents of the text file.

Syntax: Sort [-bcdfimmnr][-o< output File >][-t< separator character >][+< Start field >-< End field >][--help][--verison][file]

Supplemental Note: Sort can be sorted against the contents of a text file in units of behavior.

Parameters

-B ignores the whitespace characters that start in front of each line.

-C checks whether the files have been sorted in order.

-D sort, ignores other characters when dealing with English letters, numbers, and space characters.

When-f is sorted, lowercase letters are treated as uppercase letters.

The-I sort ignores other characters except for ASCII characters between 040 and 176.

-M merges several sorted files.

-M sorts the previous 3 letters by the initials of the month.

-N is sorted according to the size of the value.

-o< the output file > stores the sorted results into the specified file.

-R is sorted in reverse order.

-t< separator character > Specifies the field separator character to use when sorting.

The +< Start field >-< End field > is sorted by the specified field, ranging from the starting field to the previous field in the End field.

--help display Help.

--version Display version Information

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

Here are a few examples of how sort can be used.

Prints the results of each row in the text file by using the sort command. Note that the first word on line second to third of the original file is exactly the same, and the command will continue to be compared from their second word vegetables to the first character of the fruit.

$ cat Text

Vegetable Soup

Fresh vegetables

Fresh fruit

Lowfat milk

$ Sort Text

Fresh fruit

Fresh vegetables

Lowfat milk

Vegetable Soup

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 Text>result

Sorts the contents of a file example with the 2nd field as the sort key.

$ Sort +1-2 Example

For file1 and File2 file content in reverse order, the result is placed in outfile, using the first character of the 2nd field as the sort key.

$ sort-r-o outfile +1.0-1.1 Example

Sort sort is often used in pipes with other commands, combining to perform more complex functions, such as using pipelines to sort files in the current working directory to sort, with the sort key being the 6th to 8th field.

$ ls-l | Sort +5-7

$ ps-e-o "Comm pid Time" | sort-d//In alphabetical order according to the first letter of the command

The sort command can also operate on standard input. For example, if you want to combine several file lines of text and sort the merged lines of text, you can first merge multiple files with the command cat, and then use the pipe to enter the merged lines of text into the command sort,sort command to output the merged and sorted lines of text. In the following example, the file veglist and the text lines of the file fruitlist are merged and sorted to be saved in the file clist.

$ Cat Veglist fruitlist | Sort > CList

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.