A summary of sort

Source: Internet
Author: User

Recently a friend asked the sort commonly used some of the problems, although feel very simple, but still simple summed up:

1)-u: Option de-duplication

[email protected] ~]# cat seq.txt bananaappleapplepearorangeorange[[email protected] ~]# sort-u seq.txt Applebananaoran Gepear: We can see that there's only one apple showing

2)-R: Descending output

[[email protected] ~]# cat number.txt 12345[[email protected] ~]# sort-r number.txt 54321 as above: After adding the-r option, the contents of the Number.txt display are indeed Sort from high to low

3)-O option: Export

[Email protected] ~]# sort number.txt 12345[[email protected] ~]# sort number.txt >number.txt [[email protected] ~]# C at number.txt [[email protected] ~]# echo $?0 Description: Found the output number.txt file is empty [[email protected] ~]# cat number.txt [email protected  ] ~]# cat<<eof>number.txt> 1> 2> 3> 4> 5> eof[[email protected] ~]# cat Number.txt 12345[[email Protected] ~]# sort-r number.txt-o number.bak[[email protected] ~]# cat Number.bak 54321 as above: With the-o option, it is easy to save

4)-N option

Description: Confusion about sort numbers sorting

A, "sort is sometimes we think of convulsions, such as the number 11 is actually smaller than 8?" ”

B, this phenomenon is not accidental, it may be because the sort command by default is sorted by character, it would think that the character 8 in the ASCII word character is ranked after the character 1, so the 11 is ranked in front of 8

[[email protected] ~]# sort number.sort-u110111922134434458 using the-N option, sort will be sorted by value in the output [[email protected] ~]# Sort-nu Number.sort 123458101119214344

5) TK option

Description:the-t option, indicating that the specified interval character

-k option, specifying that we need to sort on that column

[[email protected] ~]# Cat fruit.txt # File Description: # The first column indicates the fruit type # The second column indicates the number of fruits # The third column represents the fruit price banana:30:5.5apple:10:2.5pear : 90:2.3orange:20:3.4[[email protected] ~]# sort-n-K 2-t: Fruit.txt # The second column indicates the number of fruits # The third column represents the fruit price # The first column indicates the fruit type # file content Description: Apple:10: 2.5orange:20:3.4banana:30:5.5pear:90:2.3[[email protected] ~]# sort-n-K 3-t: Fruit.txt # The second column indicates the number of fruits # The third column represents the fruit price # The first column indicates the fruit Type # File content Description: pear:90:2.3apple:10:2.5orange:20:3.4banana:30:5.5

6) A detailed analysis of-K

[[email protected] ~]# sort -t  '   '  -k  1 facebook.txt sort: multi-character tab  '-K ' description: Some sort may not be used directly with-t   "\ T "  let Tab be the delimiter, this time you can escape tab, for example use-t $ ' \ t '   so that you can make tab separator. [[email protected] ~]# sort -t $ ' \ t '  -k 1 facebook.txt baidu  100 5000google 110 5000sina 50 3000sohu 100  4500 The above output information may be due to the IME symbol problem caused:[[email protected] ~]# sort -t  '   '  -k 1  facebook.txt baidu 100 5000google 110 5000sina 50 3000sohu 100  4500-k 2 -k 3 (description sets the priority of sorting, first sort by 2nd column, if same, then sort by 3rd column) [[email protected] ~]#  sort -n -t  '   '  -k 2 -k 3 facebook.txt sina 50  3000sohu 100 4500baidu 100 5000google 110 5000 

Requirement: Let Facebook.txt sort according to the employee's salary descending order, for the company with the same employee salary, according to the company number ascending order:

[[email protected] ~]# cat facebook.txt Google 5000baidu 5000sina 3000sohu 4500[[email protected] ~]# sort -n-t ' k 3r-k 2 facebook.txt Baidu 5000google 5000sohu 4500sina 50 3000

Requirements: sorted by month (according to January, February, March)

[email protected] ~]# cat cal.txt januaryfebruaryjunemay[[email protected] ~]# sort-m cal.txt januaryfebruarymayjune

There are some sort of functions, because I do not have how to use, so there is no additional introduction


This article is from the "Frozen vs watermelon" blog, so be sure to keep this source http://molewan.blog.51cto.com/287340/1878320

A summary of sort

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.