Linux Sort Command

Source: Internet
Author: User

Introduction

The sort command is a text sort command that sorts standard input and text files, and can output the results through stdout, and the sort command is often used with the Uniq command. the sort command is sorted by default from the first character of each line until the end of the last character of the line (for example, the first character is the same as the second character), and the comparison is usually a comparison of numbers, in alphabetical order.

Parameters:

---------o< output File >-  -t< Delimited character >: Specifies the column separator character when sorting, by default the space is used as the delimiter;-K: Specifies the comparison column or start and end position

Test data

[[email protected] test] # Cat Test1: jan:b2: jan:a2: feb:b3: Mar:c5: may:d4:apr:e

1. Default sorting

[[email protected] test] # sort test; 1: Jan:b2: feb:b2: jan:a3: Mar:c4: apr:e5:may:d

The default is to start with the first character to compare each line, from the comparison can be seen when the first character 2 is the same, start comparing the next character compared to the third character F is in front of J

2. Reverse sort,-r

[[email protected] test] #  5: May:d4: Apr:e3: Mar:c2: jan:a2: feb:b1:jan:b

3. Sort by Month

[[email protected] test] # sort-mk 2-t: test; 1: Jan:b2: jan:a2: feb:b3: Mar:c4: apr:e5:may:d

-M: Specifies that comparisons are based on the month,-K: The column that specifies the comparison is the second column, where the-K parameter is special because it is used in conjunction with-M to explicitly compare only the second column (similar to the use of-K 2,2), and if the second column is the same again from the beginning, If the character is not explicitly specified, the default is to start comparing the specified character to the end of the line.

Note: When the delimiter in the comparison is not the default space, you need to explicitly specify the-t parameter to specify the delimiter

4.-k parameters

The-k parameter is complex, and parameters are used to specify the start and end characters of the comparison

-K Start[.start1][,end[.end1]]

Inside the brackets can be omitted,

1. If only start is specified: Represents the beginning of the first character of the start column compared to the end of the line

2. If Start,end is specified: Represents the end of a character from the start column to the last character of the end column, and if the record is identical from start to end, the match starts again from the beginning of the line.

3. If Start.start1 is specified: represents starting from the first Start1 character of the start column

[[email protected] test] # sort-k 2-t: test; 4: Apr:e2: feb:b2: jan:a1: jan:b3: Mar:c5:may:d

Note: Since only start is specified, compare the first character in the second column until the end of the line, observing the Jan two column.

[[email protected] test] # sort-k 2,2-t: test; 4: Apr:e2: feb:b1: jan:b2: jan:a3: Mar:c5:may:d

Note: Because no. start1 and. End1 are specified, compare the 2nd column first, and then, when the second column is the same, look at the Jan two column.

[[email protected] test] # sort-k 2.2,2.3-t: test; 1: Jan:b2: jan:a3: Mar:c5: May:d2: feb:b4:apr:e

Note that 1, 22 lines, because the JA is the same, and then from the beginning of the line to compare, rather than from the back of the line to compare, observe the Jan two column.

Summary

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

Linux Sort Command

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.