Linux sort command details

Source: Internet
Author: User

Detailed description of linux sort commands: sorts the content of text files. sort can sort the content of text files by behavior units. Syntax: sort [-bcdfimMnr] [-o <output file>] [-t <delimiter>] [+ <start column>-<End column>] [-- help] [-- verison] [file] 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 the version information. The-u option of sort is used to remove duplicate rows in the output row. [Root @ geralt-workpc ~] # Sort A.txt applegoldironorengesilversteelsteel [root @ geralt-workpc ~] # Sort-u A.txt applegoldironorengesilversteelsort's-r option sort is sorted in ascending order by default. If you want to change it to descending order, add-r. [Root @ geralt-workpc ~] # Sort A.txt applegoldironorengesilversteelsteel [root @ geralt-workpc ~] # Sort-r A.txt steelsteelsilverorengeirongoldapplesort's-o option outputs the sorting result to the original file [root @ geralt-workpc ~] # Sort B .txt carrotcookiesgoldorenge [root @ geralt-workpc ~] # Sort-r B .txt-o B .txt [root @ geralt-workpc ~] # Cat B .txt orengegoldcookiescarrotsort's-t option and-k option if the content of a file is like this: [root @ geralt-workpc ~] # Cat facebook.txt banana: 30: 5.5 apple: 10: 2.5 pear: 90: 2.3 orange: 20: 3.4 files have 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. Sort by the number of fruits, that is, sort by the second column. How can we use sort to achieve this? [Root @ geralt-workpc ~] # Sort-n-k 2-t: facebook.txt apple: 10: 2.5 orange: 20: 3.4 banana: 30: 5.5 pear: 90: 2.3-k option [root @ geralt-workpc ~] # Cat facebook.txt google 110 5000 baidu 100 5000 guge 50 3000 sohu 100 assume that the first domain is the company name, and the second domain is the number of people in the company, the third domain is the sorting of employees' average salaries in alphabetical order of the company [root @ geralt-workpc ~] # Sort-t ''-k 1 facebook.txt baidu 100 5000 110 google 5000 3000 guge 50 100 sohu 4500 by company count [root @ geralt-workpc ~] # Sort-n-t ''-k 2 facebook.txt guge 50 3000 baidu 100 5000 sohu 100 4500 110 google 5000 by the number of people in the company, jobs with the same number of employees are sorted in ascending order by the average salary of employees [root @ geralt-workpc ~] # Sort-n-t ''-k 2-k 3 facebook.txt guge 50 3000 sohu 100 4500 100 baidu 5000 110 5000 google sort by employee salary in descending order. If the number of employees is the same, [root @ geralt-workpc ~] # Sort-n-t ''-k 3r-k 2 facebook.txt baidu 100 5000 google 110 5000 sohu 100 4500 3000 guge 50 sorting from the second letter of the company's English name [root @ geralt-workpc ~] # Sort-t ''-k 1.2 facebook.txt baidu 100 5000 sohu 100 4500 google 110 5000 guge 50 3000-u option [root @ geralt-workpc ~] # Sort-n-k 2 facebook.txt guge 50 3000 baidu 100 5000 sohu 100 4500 google 110 5000 [root @ geralt-workpc ~] # Sort-n-k 2-u facebook.txt guge 50 3000 baidu 100 5000 google 110 5000 [root @ geralt-workpc ~] # Sort-k 1.1, 1.1-u facebook.txt baidu 100 5000 google 110 5000 sohu 100 4500 discovery-u only recognizes the domain set with-k and finds the same, delete all subsequent identical rows. [Root @ geralt-workpc ~] # Sort-n-k 2-k 3-u facebook.txt guge 50 3000 sohu 100 4500 baidu 100 5000 110 when a two-layer sorting priority is set, use-u to delete No rows. The original-u will weigh all-k options and will be deleted only when they are the same, as long as there is a level of difference between them will not be easily deleted

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.