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

Use the sort command of linux to sort files and output

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the use of linux sort command to sort files and output, the article is very detailed, has a certain reference value, interested friends must read it!

The sort command is very useful in Linux, which sorts the files and outputs the sorted results standard. The sort command can get input either from a specific file or from stdin.

Syntax format: sort [parameters] [file]

Commonly used parameters: coach

-b ignores the space characters that begin at the beginning of each line-c checks whether the file has been sorted in order-d, processes English letters, numbers and space characters, ignores other characters when sorting-f, treats lowercase letters as uppercase letters-I, except for ASCII characters between 040 and 176 Ignore other characters-m to merge files with several sort numbers-M sorts the first three letters according to the abbreviation of the month-n sort by numeric size-o stores the sorted results in the established file-r sorts in reverse order-t specifies the field separator used for sorting-k specifies the field to be sorted

Reference example

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

[root@linuxcool ~] # cat sort.txt

AA:10:1.1

CC:30:3.3

DD:40:4.4

BB:20:2.2

FF:60:6.6

FF:60:6.6

EE:50:5.5

[root@linuxcool ~] # sort sort.txt

AA:10:1.1

BB:20:2.2

CC:30:3.3

DD:40:4.4

EE:50:5.5

FF:60:6.6

FF:60:6.6

Ignore the use of the-u option or uniq:

[root@linuxcool ~] # cat sort.txt

AA:10:1.1

CC:30:3.3

DD:40:4.4

BB:20:2.2

FF:60:6.6

FF:60:6.6

EE:50:5.5

[root@linuxcool] # sort-u sort.txt

AA:10:1.1

BB:20:2.2

CC:30:3.3

DD:40:4.4

EE:50:5.5

FF:60:6.6

[root@linuxcool ~] # uniq sort.txt

AA:10:1.1

CC:30:3.3

DD:40:4.4

BB:20:2.2

FF:60:6.6

EE:50:5.5

Use of the-n,-r,-k,-t options of sort:

[root@linuxcool ~] # cat sort.txt

AA:BB:CC

Aa:30:1.9

Cc:50:3.3

Dd:20:4.2

Bb:10:2.4

Ee:40:5.3

Ee:60:5.1

[root@linuxcool ~] # sort-nk 2-t: sort.txt

AA:BB:CC

Bb:10:2.4

Dd:20:4.2

Aa:30:1.9

Ee:40:5.3

Cc:50:3.3

Ee:60:5.1

[root@linuxcool ~] # sort-nrk 3-t: sort.txt

Ee:40:5.3

Ee:60:5.1

Dd:20:4.2

Cc:50:3.3

Bb:10:2.4

Aa:30:1.9

AA:BB:CC

The above is the use of linux's sort command to sort the files and output all the contents, thank you for reading! Hope to share the content to help you, more related 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