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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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.
Nov 21, 2014 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
Jun 9 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"
.
Nov 21, 2014 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).
Nov 21, 2014 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.
Nov 21, 2014 at 10:18
sort
on your File1, with no options, I get your File2. What are you doing differently? What are you leaving out of this question?