Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the sort command of linux

Shulou Source: shulou.com Published: 2022-06-02 23:06:46 09月22日 Update

How to use the sort command of linux? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

The linux sort command is sort.

Sort compares each line of the file as a unit. The principle of comparison is to compare each other from the first character to the back, then compare them according to the ASCII code value in turn, and finally output them in ascending order.

[zookeeper@master rh] $cat seq.txtbananaapplepearorangepear [zookeeper@master rh] $sort seq.txt applebananaorangepearpear

The-u option for sort

Its simple function is to remove duplicate lines from the output line.

[zookeeper@master rh] $sort-u seq.txt applebananaorangepear

Pear was mercilessly deleted by the-u option due to repetition.

-r option for sort

[zookeeper@master rh] $cat number.txt1357112461089 [zookeeper@master rh] $sort number.txt-- the default sorting method for sort is ascending order 1101123456789 [zookeeper@master rh] $sort-n number.txt-- the sorting program sorts these numbers by characters, and the sorting program will first compare 1 and 2, obviously 1 is smaller, so put 10 before 2 1234567891011 [zookeeper@master rh] $sort-n-r number.txt-r for descending order N means to sort by number 1110987654321

Other common sort options

-f converts lowercase letters to uppercase letters for comparison, that is, ignore uppercase and lowercase

-c will check whether the files are in order, if they are out of order, output the relevant information about the first disordered line, and finally return 1

-C will check whether the files are in order, and if they are out of order, they will not output the contents, only 1 will be returned.

-M will be sorted by month, for example, JAN is less than FEB, etc.

-b ignores all white space in front of each line, starting with the first visible character.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Sort output file command line content ascending letters characters numbers programs help check clarity function information units principles uppercase uppercase letters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Shulou Information Xiaomi Docker