The sort usage of Linux

Source: Internet
Author: User

The sort command for Linux is to help us sort by different data types, its syntax and common parameter formats: sort [-bcfmnrtk][source file][-o output file]  Supplemental Description: Sort can be sorted against the contents of a text file in the unit of behavior. The parameter:  -b    ignores the space character that begins before each line.   -c    Check to see if the files are sorted in order.   -f    when sorting, ignores uppercase and lowercase letters.   -M    sorts the first 3 letters according to the abbreviation of the month.   -n    sorts according to the size of the numbers.   -o< output File >    stores the sorted results in the specified file.   -r    is sorted in reverse order.   -t< Delimited character >    specifies the field separator character to use when sorting.  -k   Choose which interval to sort by.   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ the use of sort is described below in a few examples.   (1) sort compares each line of a file as a unit, comparing it from the first character backwards, to the ASCII value in turn, and finally outputting them in ascending order. [[email protected] programming]$ cat seq.txtbananaapplepearorange[[email protected] programming]$ sort Seq.txtapplebananaorangepear users 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's-u option It's a simple function of removing duplicate rows in the output line. [[email protected] programming]$ cat Seq.txtbananaapplePearorangepear[[email protected] programming]$ sort seq.txtapplebananaorangepearpear[[email protected] programming]$ sort-u Seq.txtapplebananaorangepearpear was ruthlessly removed by the-u option because of repetition. (3) the-R option for sort is sorted by default in ascending order, and if you want to change to descending order, add an-R to it. [[email protected] programming]$ cat number.txt13524[[email protected] programming]$ sort number.txt12345[ [email protected] programming]$ sort-r number.txt54321 (5) Sort's-o option because sort defaults to outputting the results to standard output, a redirect is required to write the results to a file, such as the 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, the number was emptied. At this point, the-O 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.txt13524[[email protected] programming]$ sort-r number.txt-o Number.txt[[email protected] programming]$ cat number.txt54321 (6)  sort-n option have you ever encountered 10:2 small cases. I've met anyway. This occurs because the sorting program sorts the numbers by character,The sorting program compares 1 and 2, which is obviously 1 small, so put 10 in front of 2. This is also the sort's consistent style. If we want to change this situation, we need to use the-n option to tell sort, "Sort by value"! [[email protected] programming]$ cat number.txt110191125[[email protected] programming]$ sort Number.txt110111925[[email protected] programming]$ sort-n number.txt125101119 (7)   Sort the-t option and the-K option if there is a file with the contents of this: [[email protected] programming]$ cat Facebook.txtbanana:30:5.5apple:10:2.5pear : 90:2.3orange:20:3.4 This file has three columns, separated by a colon between columns and columns, the first column represents the fruit type, the second column indicates the number of fruits, 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.txtapple:10:2.5orange:20:3.4banana:30:5.5pear : 90:2.3 (8)   Other sort common options-f converts lowercase letters to uppercase to compare, that is, ignore case-C checks if the file is ordered, if it is out of order, it prints the information about the first disorderly row, and finally returns 1-C checks to see if the file is ordered. If the order is not output, only the returned 1-m will be sorted by month, for example, Jan is less than Feb and so-B ignores all the blank parts preceding each line, starting with the first visible character.  

Sort usage of Linux

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.