The Linux type command can prepare command output or file content material in much more methods than you would possibly realize–alphabetically, numerically, by month and randomly are solely among the extra fascinating decisions. On this submit, we check out among the extra helpful sorting choices and clarify how they differ.
The default
The default type might sound pretty simple. Digits come first, adopted by letters and, for every letter, lowercase characters precede uppercase characters. You may anticipate to see this sort of ordering:
012345aAbBcCdDeE
ASCII order
Trying on the numeric byte values for every of those letters, it’s possible you’ll word that what you see above is just not the “pure order” so far as ASCII is worried.
$ echo 012345aAbBcCdDeE | od -bc 0000000 060 061 062 063 064 065 141 101 142 102 143 103 144 104 145 105 0 1 2 3 4 5 a A b B c C d D e E
As you may discover on this octal dump of the checklist of characters, uppercase letters have decrease ASCII values and would come earlier than lowercase letters in the event that they have been listed in ASCII order. To type by byte worth, prepend your type command with LC_ALL=C. For instance, here is a comparability of sorting in byte order in contrast with the default type order:
$ LC_ALL=C type file $ type file 0 0 1 1 2 2 3 3 4 4 5 5 A <== a <== B <== A <== C b D B E c a C b d c D d e e E
Numeric order
To type numerically, it’s essential use -n otherwise you’ll find yourself sorting numbers by character and 100 would fake to be smaller than 2. This is a comparability between a default type and a numeric type:
$ type numbers $ type -n numbers 0 0 1 1 11 4 4 9 44 11 9 44
You too can type numerically utilizing a “human-friendly” type order. This lets you characterize numbers with greater than digits–such as 5M. The choice for this type order is -h. Once you use it, 5K could be handled as bigger than 500 and fewer than 5M. This is a comparability of the default type and a human-friendly type:
$ type numbers $ type -h numbers 0 0 1 1 11 4 4 9 44 11 500 44 5K 500 5M 5K 9 5M
By Month
To type by month identify, you’ll use the -M possibility. This is an instance of a default type and a kind by month:
$ type months # type -M months Apr Jan Aug Feb Dec Mar Feb Apr Jan Might Jul Jun Jun Jul Mar Aug Might Sep Nov Oct Oct Nov Sep Dec
Discover that sorting by month works whether or not you spell out the names of the months or use abbreviations:
$ type -M months2 Jan Feb March Apr Might June Jul August Sep October November Dec
Perceive {that a} type by month is not a kind by date. This kind possibility assumes that every one months are in the identical 12 months.
$ type occasions $ type -M occasions Feb 10 2020 20:06 SOMETHING Jan 23 2020 10:42 SOMETHING Feb 11 2020 20:06 SOMETHING Jan 29 2020 09:17 SOMETHING Feb 12 2019 11:11 SOMETHING Feb 10 2020 20:06 SOMETHING Feb 27 2020 23:05 SOMETHING Feb 11 2020 20:06 SOMETHING Jan 23 2020 10:42 SOMETHING Feb 12 2019 11:11 SOMETHING <== Jan 29 2020 09:17 SOMETHING Feb 27 2020 23:05 SOMETHING <== Jun 26 2019 09:09 SOMETHING Jun 26 2019 09:09 SOMETHING
Reversing listings
To reverse the order of your sorted listings, add the -r possibility. This is a reverse itemizing of the months and human-readable numbers recordsdata:
$ type -Mr months $ type -hr numbers Dec 5M Nov 5k Oct 500 Sep 44 Aug 11 Jul 9 Jun 4 Might 1 Apr 0 Mar Feb Jan
Random sorting
To type textual content in a pseudorandom vogue, use -R together with your type command. Listed below are among the earlier kinds utilizing the random possibility.
$ type -R months $ type -R numbers Aug 500 Nov 4 Dec 44 Sep 5M Apr 0 Jan 1 Jul 5K Jun 11 Might 9 Mar Feb Oct
The opposite option to type knowledge randomly is to make use of the shuf (for “shuffle”) command. Listed below are a pair examples utilizing knowledge from earlier examples on this submit:
$ shuf months $ shuf numbers Nov 0 Jun 4 Might 500 Aug 5K Apr 11 Dec 44 Jul 1 Feb 9 Mar 5M Oct Sep Jan
Sorting Command Output
You too can pipe knowledge to any of the type instructions proven. The command under won’t be notably helpful, however it demonstrates the purpose and reveals another instructions associated to sorting.
$ apropos type | type -r XConsortium (7) - X Consortium data versionsort (3) - scan a listing for matching entries tsort (1) - carry out topological type type (1) - type traces of textual content recordsdata qsort_r (3) - type an array qsort (3) - type an array comm (1) - examine two sorted recordsdata line by line bzip2 (1) - a block-sorting file compressor, v1.0.8 bunzip2 (1) - a block-sorting file compressor, v1.0.8 bsearch (3) - binary search of a sorted array apt-sortpkgs (1) - Utility to type bundle index recordsdata alphasort (3) - scan a listing for matching entries
Copyright © 2020 IDG Communications, Inc.
Leave a Reply