Use of SORT commands in Linux

Source: Internet
Author: User
Tags comparison sort sorts linux

The sort command's function is to sort the rows in a file. The sort command has a number of very useful options, which were initially used to sort through various sorts of file contents in a database format. In fact, the sort command can be considered a very powerful data management tool for managing files that have content similar to database records.

The sort command sorts the contents of the file one line at a time, and if the first character of the two rows is the same, the command continues to compare the next character of the two lines, and if it is the same, the comparison continues.

Grammar:

Sort [Options] File

Description: The sort command sorts all the rows in the specified file and displays the results on the standard output. If you do not specify an input file or use "-", it means that the sorted content is from standard input.

Sort sort is done by comparing one or more keywords extracted from an input row. The sort key defines the smallest sequence of characters used to sort. By default, the integer behavior keyword is sorted in ASCII character order.

The main options for changing the default settings are:

-M if a given file is sorted, merge the file.

-C Check if the given file is sorted and if they are not all sorted, print an error message and exit with a status value of 1.

-U leave only one row for the rows that you think are the same after sorting.

-o The output file writes the sorted output to the output file rather than the standard output, and if the output file is one of the input files, sort first writes the contents of the file to a temporary file and then sorts and writes out the output.

The main options for changing the default collation are:

-D is sorted in dictionary order, and only letters, numbers, spaces, and tabs are meaningful when compared.

-F Treats lowercase letters as if they were uppercase.

-I Ignore nonprinting characters.

-M as Month comparison: "The FEB" < "the"

-R Sorts the results in reverse order output.

+POSL-POS2 specifies one or more fields as the sort key, and the field position starts at POSL, until Pos2 (including POSL, excluding Pos2). If you do not specify POS2, the keyword is from POSL to line end. The field and character positions start at 0.

-B ignores leading whitespace (spaces and tabs) when looking for a sort key in each row.

-T separator Specifies the character separator as the field separator.

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.