Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to sort text efficiently in linux

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to sort the text efficiently in linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Sort in dictionary order

If there is text test1.txt as follows (secretly ask: which linux distribution are you using? ):

DebianGentooGentooCentOSFedoraubuntuKaliredhatArchOpenSuse

Now to sort the text in ascending dictionary order, simply use the following command:

Sort test1.txt

The output is as follows:

ArchCentOSDebianFedoraGentooGentooKaliOpenSuseredhatubuntu

You can see that the output is sorted in ascending dictionary order. If you need descending output, you only need to use the-r parameter, such as:

Sort-r test1.txt

The output is as follows:

UbunturedhatOpenSuseKaliGentooGentooFedoraDebianCentOSArch

Remove duplicate lines

We see that the first two lines are duplicated, that is, the Gentoo line, which can be removed by using the-u parameter:

Sort-u test1.txt

The output is as follows:

ArchCentOSDebianFedoraGentooKaliOpenSuseredhatubuntu

In the output, duplicate Gentoo lines are removed.

Sort by number

Suppose there is a text test2.txt as follows: the first column is the market capitalization ranking, the second is the company name, and the third is the company creation time:

2 google 199810 icbc 19845 tencent 19981 apple 197616 samsung 1938

According to the previous introduction, we sort the content as follows:

10 icbc 198416 samsung 19381 apple 19762 google 19985 tencent 1998

This is not right. Why is 16 in front of 1? Obviously this is because the previous sort is a dictionary sort, and what we really need is to sort by numeric size, so we need to use the-n parameter:

Sort-n test2.txt

The final output is as follows:

1 apple 19762 google 19985 tencent 199810 icbc 198416 samsung 1938

As you can see, the final result is printed according to the market capitalization ranking.

Sort by specified column

What if we don't want to rank by market capitalization, but by company name? Then you need to sort by the specified column. Two parameters are needed here:

-t specifies the delimiter. When not specified, the default delimiter is blank

-k specify column sort

You need to sort by the second column, the company name, so the sort command is as follows:

Sort-k 2 test2.txt # defaults to white space as the delimiter

Sort-k 2-t''sort2.txt # delimited by a space

The results are as follows:

1 apple 19762 google 199810 icbc 198416 samsung 19385 tencent 1998

As you can see, the output is sorted based on the second column, the company name.

Sort by multiple columns

Suppose we sort by the year in which the company was created, and if the year is the same, sort by company name, that is, by the third column and the second column by:

Sort-n-k 3-k 2 test2.txt

The output is as follows:

16 samsung 19381 apple 197610 icbc 19842 google 19985 tencent 1998

If you want to sort by year in descending order, simply add the-r parameter:

Sort-n-k 3r-k 2 test2.txt

The output is as follows:

2 google 19985 tencent 199810 icbc 19841 apple 197616 samsung 1938

Sort by specific characters of a specific column

If we want to sort by the second to third characters of the company name, we can use the following ways:

Sort-t''- k 2.2 test2.txt

The output is as follows:

16 samsung 193810 icbc 19845 tencent 19982 google 19981 apple 1976

-k 2.2 not 2.3 specifies that it is sorted by the second column and starts with the second character of the second column and ends with the third character. As you can see, samsung begins because the second letter of the letter is a. If you start with the second character and end with the last character, use the following command:

Sort-t'- k 2.2 test2.txt

Save sort results

The previous sort command simply prints the sort results to the console and does not modify the source file. If you want to save the sort results in the file, you need to use the-o parameter:

Sort test1.txt-o test1.txt # directly modify the source file sort test1.txt-o output.txt # output to another file sort test1.txt > output.txt # redirect to output.txt

If you want to save the sort results in the source file, you can only use the-o parameter, and for other files, you can use either the-o parameter or redirection.

Check if it is out of order

Sort can also be used to check whether the text content is sorted.

For example:

Sort-c test1.txt # prints the result and tells the number of lines starting out of order sort-C test1.txt # does not print the result, but the command returns a result of 1

Merge sorted text

You can use the-m option to merge ordered text, but not reorder it.

For example:

Sort-m file1 file2

Summary

These are common ways to sort text using the sort command, and more can be seen using the man command. Common options are as follows:

-r arrange in descending order-n arrange in numerical size-k arrange in specified column-t specify delimiter-u remove weight-o output the result to the file above is all the content of this article "how to sort text efficiently in linux", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report