The situation is only marginally improved when using the --numeric-sort (-n) option.
$ sort -n addresses.txt
1.2.3.4
5.220.100.50
5.24.69.2
19.20.203.5
19.20.21.22
129.95.30.40
The first set of numbers in each dotted-quad sort correctly—5 preceeds 19, and 129 is at the tail end—but the internal numbering still gets improper treatment. 5.220.100.50 is listed prior to 5.24.69.2 because 220 is alphabetically prior to 24. Likewise the two 19.20.x.x addresses are mixed up because 203 is alphabetically prior to 21.
The solution is to tell sort to order the list numerically, considering each address as a set of four numeric fields, each separated by a dot.
$ sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 addresses.txt
1.2.3.4
5.24.69.2
5.220.100.50
19.20.21.22
19.20.203.5
129.95.30.40
In English, you’re saying, “Yo, sort! I’ve got here a list of numbers (-n), but each item in the list consists of some subnumbers, fields set apart from the others by a dot (-t .). Sort first by the first field, and only the first field (-k 1,1), then by the second and only the second (-k 2,2), and so on (-k 3,3 -k 4,4).
- anr blogja
- A hozzászóláshoz be kell jelentkezni
- 655 megtekintés
Hozzászólások
Ez tetszik :)
____________________________________
Az embert 2 éven át arra tanítják hogyan álljon meg a 2 lábán, és hogyan beszéljen... Aztán azt mondják neki: -"Ülj le és kuss legyen!"..
- A hozzászóláshoz be kell jelentkezni
Egesz veletlenul a nonGNU sort is tudja pontosan ugyanezt.
Ja, es lustaknak: sort-nal sem kotelezo az opcio utani szokoz:
$ sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 addresses.txt
- A hozzászóláshoz be kell jelentkezni
Ezt jo tudni.
Alairas: Egy lusta. :-)
--
()=() Ki oda vagyik,
('Y') hol szall a galamb
C . C elszalasztja a
()_() kincset itt alant.
- A hozzászóláshoz be kell jelentkezni
Pont ugyanezt akartam írni :-) (no, jó, a lustákkal nem törődtem volna)
- A hozzászóláshoz be kell jelentkezni
Ezt már rég ismertem, amikor verziószámokat kellett "időrendbe" rendeznem, akkor találtam meg ezeket (a honlapomon az lfs szkriptek között érdemes kutakodni, ott használom, a pkgmgr szkriptben benne is van).
- A hozzászóláshoz be kell jelentkezni