The sort usage of Linux

Source: Internet
Author: User
Tags pear sorts

The sort command is to help us sort by different data types, its syntax and common parameter formats:
  sort [-bcfmnrtk][source file][-o output file]
Additional notes: Sort can be sorted for the content of the text file, in the unit of behavior.

Parameters
-B ignores whitespace characters that begin before each line.
-C checks whether the files are sorted in order.
-f sort, ignores uppercase and lowercase letters.
-M sorts the first 3 letters according to the abbreviation of the month.
-N Sorts by the size of the numbers.
-o< the output file > The sorted result into the specified file.
-R is sorted in reverse order.
-t< Delimited character > specifies the field separator character to use when sorting.
-K selects which interval to sort.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


(1) sort takes each line of the file as a unit, comparing it to each other, and comparing the principle from the first character backwards, Compare them by ASCII values, and then output them in ascending order.

[email protected] programming]$ cat Seq.txt
Banana
Apple
Pear
Orange
[Email protected] programming]$ sort Seq.txt
Apple
Banana
Orange
Pear

The user 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) the-u option for sort

It is simple to remove duplicate rows in the output line.

[[email protected] programming]$ cat seq.txt
APPLE
pear
orange
PEAR
[[email protected] programming]$ sort seq.txt
APPLE
banana
orange
PEAR
pear
[[email protected " programming]$ sort-u seq.txt
banana< BR style= "margin:0px;padding:0px;" >ORANGE
pear

Pear was ruthlessly removed by the-u option because of repetition.

(3) the-R option for sort

Sort by default is in ascending order, if you want to change to descending order, add an-R to get it done.

[email protected] programming]$ cat Number.txt
1
3
5
2
4
[Email protected] programming]$ sort Number.txt
1
2
3
4
5
[Email protected] programming]$ sort-r number.txt
5
4
3
2
1
(5) The-O option for sort

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]$
Look, 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.txt
3
5
2
4
[[email protected "programming]$ Sort-r number.txt-o number.txt
5
4
3
2
1

(6) The-N option for sort

Have you ever encountered 10:2 small cases. I've met anyway. This occurs because the sorting program sorts the numbers by character, and the sorting program compares 1 and 2, which is obviously 1 small, so 10 is placed before 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.txt
1
10
19
11
2
5
[Email protected] programming]$ sort Number.txt
1
10
11
19
2
5
[Email protected] programming]$ sort-n number.txt
1
2
5
10
11
19

(7)-t option for sort and-k option

If there is a file with the contents of this:

[email protected] programming]$ cat Facebook.txt
banana:30:5.5
apple:10:2.5
pear:90:2.3
orange: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] programming]$ sort-n-K 2-t ': ' Facebook.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3

(8) 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.


This article is from the "XFICC" blog, make sure to keep this source http://xficc.blog.51cto.com/1189288/1565161

Sort usage of Linux

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.