I have file1:
"$lvl=57"
"$lvl=20"
"$lvl=48"
"$lvl=17"
"$lvl=58"
File2 I want:
"$lvl=17"
"$lvl=20"
"$lvl=48"
"$lvl=57"
"$lvl=58"
Basically numerically sort of file1.
I like the -V
/ --version-sort
option found in a few implementations of sort
(from GNU sort
): it behaves very well for many situations mixing strings and numbers
sort -V
I use this option very often...
In the same direction, with some implementations of ls
, ls -v
for version-sort ls
(from GNU ls
).
You need to tell sort -n
to sort on the part after the =
:
sort -t = -k 2n
-k
defines a sort key. See the man page for details. -k2n
defines a sort key starting at the second field and ending at the end of the line and makes it a numeric sort key.
– Stéphane Chazelas
Nov 21 '14 at 10:45
I found that, you just run sort -h
, it will work. They call it --human-numberic-sort
.
sort -h file1 > file2
-h
and --human-numeric-sort
– user598527
Jun 09 '23 at 11:31
use sort:
sort -n file1 > file2
-n, --numeric-sort
compare according to string numerical value
sort -g file1 > file 2
-g, --general-numeric-sort
sort -n
since they don't start with a number. The reason it sorts them is the last-resort full-line sort (lexically, not numerically) done for lines with the same ranking. That would sort "$lvl=17"
before "$lvl=2"
.
– Stéphane Chazelas
Nov 21 '14 at 10:02
-n
and -g
are redundant here as the input is not numerical. So this answer is misleading, hence the downvote (also note that -g
and the long options are GNU specific).
– Stéphane Chazelas
Nov 21 '14 at 10:15
-n
is that it's not limited to decimal integers. That would still sort "$lvl=17"
before "$lvl=2"
as part of the last-resort sorting.
– Stéphane Chazelas
Nov 21 '14 at 10:18
-g
is not a general-purpose numerical sort. It's much slower than the plain -n
numeric sort, but -g
can handle scientific notation, which is not needed here. See the info page of sort for more details.
– PM 2Ring
Nov 21 '14 at 10:19
sort
on your File1, with no options, I get your File2. What are you doing differently? What are you leaving out of this question? – Scott - Слава Україні Sep 28 '17 at 04:49