Linux Sort command

Source: Internet
Author: User
Tags pear

1, sort the file each line as a unit, compared to each other, the comparison principle is from the first character backward, followed by the ASCII value of the comparison, and finally they are output in ascending order.

?
1 2 3 4 5 6 7 8 9 10 [[email protected] ~]# cat seq.txt  banana apple pear orange [[email protected] ~]# sort seq.txt   #默认升序 apple banana orangepear

2. Sort-u option---Remove duplicate rows

?
1 2 3 4 5 6 7 8 9 10 11 12 13 [[email protected] ~]# cat seq.txt banana apple   pear orange pear [[email protected] ~]# sort -u seq.txt   apple banana orangepear

3. Sort-r option----Descending

?
1 2 3 4 5 6 7 8 9 10 11 [[email protected] ~]# sort -u seq.txt   apple banana orange pear [[email protected] ~]# sort -ur seq.txt pear orange bananaapple

4. Sort-o option----Redirect, same as > under Linux

[[Email protected] ~]# Cat SeqNO.txt12345[[Email protected]~]# Sort-r seqNO.txt >Number.txt[[email protected]~]# Cat Number.txt54321[[Email protected]~]# Sort-r SeqNO.txt-o number2.txt[[email protected]~]# Cat Number2.txt54321

5. Sort-n option---Sort by value

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8 5 86 87 88 [[email protected] ~]# cat seq2    #排序程序将这些数字按字符来排序了,排序程序会先比较1和2,所以下面的升序中1反而最大。 1 2 5 8 10 11 100 [[email protected] ~]# sort seq2                                                                                          1 10 100 11 2 5 8 [[email protected] ~]# sort -r seq2 8 5 2 11 100 10 1 [[email protected] ~]# sort -n seq2  #-n以数值排序 1 2 5 8 10 11 100 [[email protected] ~]# sort -nr seq2 100 11 10 8 5 2 1 [[email protected] ~]# cat seq2 1 2 5 8 10 11 100 apple banana peal orange [[email protected] ~]# sort -n seq2  #当排序文件有字符和数字,先排序字符,再排序数字 apple banana orange peal 1 2 5 8 10 11 100 [[email protected] ~]# sort -r seq2 peal orange banana apple 8 5 2 11 100 10 1 [[email protected] ~]# sort -rn seq2 100 11 10 8 5 2 1 peal orange banana apple

6, sort-t-k option---t specifies the delimiter,-k specifies which column to sort

?
1 2 3 4 5 6 7 8 9 10 [[email protected] ~]# cat score  #共三列,假使:第一列表示姓名,第二列表示数学分数,第三列表示英语分数 Rusky,80, 90 Jack ,90, 100 Anna ,75, 80 Tom  ,88, 70 [[email protected] ~]# sort -n -k 2 -t , score   #以第二列数学分数重新排序,分隔符为逗号 Anna ,75, 80 Rusky,80, 90 Tom  ,88, 70Jack ,90, 100

[[email protected] ~]# sort-n-k 2-t ' Score2 #如果分隔符为空格, use single quotes, with spaces in the middle
Anna 75 80
Rusky 80 90
Tom 88 70
Jack 90 100

[Email protected] ~]# Sort-k 1 score
Anna, 75, 80
Jack, 90, 100
RUSKY,80, 90
Tom, 88, 70

7 Other sort common options

-F converts lowercase letters to uppercase for comparison, i.e. ignores case

-C checks to see if the file is ordered, and if it is out of order, it outputs the information about the first scrambled row, and finally returns 1

-C Checks if the file is ordered, if it is not output, returns only 1

-M is sorted by month, for example, Jan is less than Feb and so on

-B ignores all the blank parts preceding each line, starting with the first visible character.

Sort by numeric value, second column, and third column if the second column is the same [[email protected]~]# Cat Score2rusky the   -Jack -   -Anna the   theTom the   -[[Email protected]~]# Sort-n-K2-K3-T' 'Score2anna the   theTom the   -Rusky the   -Jack -   -
sort in reverse order of third column values [[email protected]~]# sort-t' '-k 3nr Score2 Jack -   -Rusky the   -Tom the   -Anna the   thesort in reverse order of the second column value, if same value, sort in ascending order of the third column [email protected]~]# sort-t' '-K 2NR-k 3n Score2jack -   -Tom the   -Rusky the   -Anna the   the

"From:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html" 

  

Related Article

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.