In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "linux sorts with sort on the command line". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "linux sorts with sort on the command line".
Installation
You don't have to install sort because it's always included in POSIX systems. In most Linux systems, the sort command comes from the collection of utilities packaged by the GNU organization. On other POSIX systems, such as BSD and Mac, the default sort command is not provided by GNU, so some options may be different. In this article, I try to explain the implementation of both GNU and BSD.
Arrange rows in alphabetical order
By default, the sort command reads the first character of each line of the file and sorts each line in ascending alphabetical order before output. If the first character in the two lines is the same, the next character is compared. For example:
$cat distro.listSlackwareFedoraRed Hat Enterprise LinuxUbuntuArch1337MintMageiaDebian$ sort distro.list1337ArchDebianFedoraMageiaMintRed Hat Enterprise LinuxSlackwareUbuntu
Using sort does not change the original file. Sort only serves as a filter, so if you want to save the data in a sorted format, you need to redirect it with > or tee.
$sort distro.list | tee distro.sorted1337ArchDebian [...] $cat distro.sorted1337ArchDebian [...] Sort by column
Complex datasets sometimes need to sort more than the first character of each row. For example, suppose you have a list of animals, each of which has its own species and genus, separating each "field" (that is, the "cell" in the data table) with a predictable separator. Such formats derived from datasheets are common, and the CSV (comma-delimited data comma-separated values) suffix can identify these files (although CSV files are not necessarily separated by commas, and delimited files do not necessarily use the CSV suffix). The following data is used as an example:
Aptenodytes;forsteri;Miller,JF;1778;EmperorPygoscelis;papua;Wagler;1832;GentooEudyptula;minor;Bonaparte;1867;Little BlueSpheniscus;demersus;Brisson;1760;AfricanMegadyptes;antipodes;Milne-Edwards;1880;Yellow-eyedEudyptes;chrysocome;Viellot;1816;Southern RockhopperTorvaldis;linux;Ewing,L;1996;Tux
For this set of sample data, you can use-- field-separator (use-t in BSD and Mac, or the abbreviation-t on GNU) to set the delimiter as a semicolon (because the sample data uses a semicolon instead of a comma, theoretically the delimiter can be any character), and the-- key (use-k on BSD and Mac, or the abbreviation-k on GNU) option to specify which field is sorted. For example, sort the second field of each row (the count starts with 1 instead of 0):
Sort-- field-separator= ";"-- key=2Megadyptes;antipodes;Milne-Edwards;1880;Yellow-eyedEudyptes;chrysocome;Viellot;1816;Sothern RockhopperSpheniscus;demersus;Brisson;1760;AfricanAptenodytes;forsteri;Miller,JF;1778;EmperorTorvaldis;linux;Ewing,L;1996;TuxEudyptula;minor;Bonaparte;1867;Little BluePygoscelis;papua;Wagler;1832;Gentoo
The results are a bit difficult to read, but Unix is known for constructing the pipeline of commands, so you can use the column command to beautify the output. Use GNU column:
$sort-field-separator= ";"\\-key=2 penguins.list | column-- table-- separator ";" Megadyptes antipodes Milne-Edwards 1880 Yellow-eyedEudyptes chrysocome Viellot 1816 Southern RockhopperSpheniscus demersus Brisson 1760 AfricanAptenodytes forsteri Miller,JF 1778 EmperorTorvaldis linux Ewing,L 1996 TuxEudyptula minor Bonaparte 1867 Little BluePygoscelis papua Wagler 1832 Gentoo "
It may be a little difficult for beginners to understand (but easy to write), the command options on BSD and Mac:
$sort-t ";"\-K2 penguins.list | column-t-s ";" Megadyptes antipodes Milne-Edwards 1880 Yellow-eyedEudyptes chrysocome Viellot 1816 Southern RockhopperSpheniscus demersus Brisson 1760 AfricanAptenodytes forsteri Miller,JF 1778 EmperorTorvaldis linux Ewing,L 1996 TuxEudyptula minor Bonaparte 1867 Little BluePygoscelis papua Wagler 1832 Gentoo "
Of course,-k doesn't have to be set to 2. Any field that exists can be set as a sorted key.
In reverse order
You can reverse the sorted list with the option-- reverse (for BSD/Mac-- r Magna GNU can also be abbreviated-r).
$sort-- reverse alphabet.listzyxw [...]
You can also pipe the output to the command tac to achieve the same effect.
Sort by month (only supported by GNU)
Ideally, everyone would write the date according to the ISO 8601 standard: year, month, and day. This is a logical way to specify a precise date and can be easily understood by a computer. In many cases, humans mark dates in other ways, including months with random names.
Fortunately, the GNU sort command recognizes this writing and sorts it correctly by the name of the month. Use the-- month-sort (- M) option:
$cat month.listNovemberOctoberSeptemberApril [...] $sort-- month-sort month.listJanuaryFebruaryMarchAprilMay [...] NovemberDecember
Both the full name and abbreviation of the month can be identified.
Human readable numeric ordering (supported by GNU only)
Another common confusion between humans and computers is the combination of numbers. For example, humans usually write "1024 kilobytes" as "1KB" because it is easier and faster for humans to parse "1KB" than "1024" (the larger the number, the more obvious the difference). For a computer, a string of 9 KB is larger than a string such as 1 MB (although 9 KB is a small part of 1 MB). The GNU sort command provides the-- human-numeric-sort (- h) option to help parse these values correctly.
$cat sizes.list2M12MB1k9k9007000 $sort-- human-numeric-sort90070001k9k2M12MB
There are some exceptions. For example, "16000 bytes" is larger than "1 KB", but sort does not recognize it.
$cat sizes0.list2M12MB160001k$ sort-h sizes0.list160001k2M12MB
Logically, 16000 of this example should be written as 16 KB, so it should not all be blamed on GNU sort. As long as you make sure the numbers are consistent, human-numeric-sort can parse them into human-readable numbers in a computer-friendly way.
Random sort (only supported by GNU)
Sometimes tools also provide options that run counter to the original intention of the design. To some extent, the sort command provides the ability to randomly sort a file without any sense. The workflow of this command makes this feature convenient. You can use other commands, such as shuf, or you can add an option with the current command. Whether you think it's a bloated or creative user experience design, the GNU sort command provides the ability to randomly sort files.
The purest random sort format option is-- random-sort or-R (don't be confused with-r, which is short for-- reverse).
$sort-- random-sort alphabet.listdmpa [...]
Each time you run a random sort on a file, you get a different result.
Thank you for your reading, the above is the content of "linux sorts with sort on the command line". After the study of this article, I believe you have a deeper understanding of the problem of linux sorting on the command line with sort, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.