Linux command sort usage

Source: Internet
Author: User
Tags printable characters alphanumeric characters

Linux command sort usage

Sort is a very common command in Linux, a sort of tube
View sort usage with man sort

-B,--ignore-leading-blanks
Ignore leading blanks

-D,--dictionary-order
Consider only blanks and alphanumeric characters

-F,--ignore-case
Fold lower case-to-upper case characters

-G,--general-numeric-sort
Compare according to general numerical value

-I.,--ignore-nonprinting
Consider only printable characters

-M,--month-sort
Compare (unknown) < ' JAN ' < ... < ' DEC '

-H,--human-numeric-sort
Compare human readable numbers (e.g., 2K 1G)

-N,--numeric-sort
Compare according to string numerical value

-R,--random-sort
Sort by random hash of keys

--random-source=file
Get random bytes from FILE

-R,--reverse
Reverse the result of comparisons
1. Sort working principle
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] ~]# cat Seq.txt nanaapplepearorangemageedu

2, Sort-u
The simple function is to remove duplicate rows in the output line.

[email protected] ~]# cat Seq.txt nanaapplepearorangemageeduapple
[Email protected] ~]# sort-u seq.txt applemageedunanaorangepear

Apple was ruthlessly removed by the-u option because of repetition.
3. Sort's-r option in descending order
Sort by default is in ascending order, if you want to change to descending order, add an-R to get it done.

[email protected] ~]# cat Seq.txt nanaapplepearorangemageeduapple
[Email protected] ~]# sort Seq.txt appleapplemageedunanaorangepear
[Email protected] ~]# sort-r seq.txt pearorangenanamageeduappleapple

4. The-O option for sort
Sort defaults to output the results to standard output, so a redirect is required to write the results to a file, like the sort filename > NewFile.

[Email protected] ~]# sort seq.txt > Ssssss.txt
[email protected] ~]# cat Ssssss.txt appleapplemageedunanaorangepear

However, if you want to output the sorting results to the original file, redirection is not possible.

[Email protected] ~]# sort ssssss.txt > ssssss.txt [[email protected] ~]# cat ssssss.txt [[email protected] ~]#

The contents of the Ssssss.txt were emptied.

[email protected] ~]# cat Number.txt 13524697

Add-o parameter and descending sort result is:

[Email protected] ~]# sort-r number.txt-o number.txt
[email protected] ~]# cat Number.txt 97654321

Now try to see what happens when the contents are letters instead of numbers.

[email protected] ~]# cat Seq.txt appleapplemageedunanaorangepear
[Email protected] ~]# sort-r seq.txt-o seq.txt
[email protected] ~]# cat Seq.txt pearorangenanamageeduappleapple

The result is right, proved successful OH.
5. Sort-n Parameters
Have you ever encountered 10:2 small cases. I've met anyway. 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.
For example:

[email protected] ~]# cat Test.txt 1023043461
[Email protected] ~]# sort Test.txt 1102303446

You see the results? Anyway I saw, 2:10 is still in front, haha.
What happens when I add the-n parameter below?

[Email protected] ~]# sort-n test.txt 1246103034

Ok!!!!!!!!!!!!

6, Sort-t and-K usage

[email protected] ~]# cat Dddd.txt banana:30:5.5apple:10:2.5pear:90:2.3orange:20:3.4

This file has three columns, separated by a colon between the column and column, the first column indicates the fruit type, the second column represents the fruit quantity, 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] ~]# sort-n-K 2-t ': ' Dddd.txt apple:10:2.5orange:20:3.4banana:30:5.5pear:90:2.3
[Email protected] ~]# sort-n-K 3-t ': ' Dddd.txt pear:90:2.3apple:10:2.5orange:20:3.4banana:30:5.5

Sort also has a lot of usage, follow-up in Add, thank you for your attention.





This article is from the "Liang blog" blog, make sure to keep this source http://7038006.blog.51cto.com/7028006/1812529

Linux command sort usage

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.