sorting numbers with the sort command - sorting

I would like to sort the values in the following file numbers.txt, below is an example
1.1
1.2
10.0
2.2
1000.0
However when I execute the following cmd
sort numbers.txt
I get the following:
1.1
1.2
10.0
1000.0
2.2
What cmd to I need to sort the numeric values appropriately?

Use the -n flag to sort numerically, instead of lexicographically:
sort -n numbers.txt

Related

Sort set of string elements in descending order using shell script

I am trying to sort set of string elements in descending order but the result surprises me. I can see 7.1.1 comes before 7.10.1, not sure why.
$ cat sort.sh
#!/bin/bash
for i in $( echo "5.2.0 5.1.2 7.1.1 7.10.1" | tr ' ' '\n' | sort -r )
do
echo $i
done
Output:
$ ./sort.sh
7.1.1
7.10.1
5.2.0
5.1.2
Expected output:
$ ./sort.sh
7.10.1
7.1.1
5.2.0
5.1.2
Am i missing anything here? Any tips would be really helpful.
Thanks
Sort uses the sorting rules specified by the current locale. See "Sort does not sort in normal order!" in GNU Coreutils FAQ. GNU sort (1) manual page contains this warning:
*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.
If you specifically want to sort version numbers, see the -V command line option in GNU sort:
-V, --version-sort
natural sort of (version) numbers within text

bash sort alphanumeric buildnumber

I have a list of buildnumbers which I get from my buildserver, like this:
1.0.0.b1
1.0.0.b10
1.0.0.b11
1.0.0.b12
1.0.0.b13
1.0.0.b14
1.0.0.b15
1.0.0.b16
1.0.0.b17
1.0.0.b18
1.0.0.b19
1.0.0.b2
1.0.0.b20
1.0.0.b21
1.0.0.b22
1.0.0.b3
1.0.0.b4
1.0.0.b5
1.0.0.b6
1.0.0.b7
1.0.0.b8
1.0.0.b9
now I need to sort this where I expect the highes buildnumber on the bottom like this:
1.0.0.b1
1.0.0.b2
1.0.0.b3
1.0.0.b4
1.0.0.b5
1.0.0.b6
1.0.0.b7
1.0.0.b8
1.0.0.b9
1.0.0.b10
1.0.0.b11
1.0.0.b12
1.0.0.b13
1.0.0.b14
1.0.0.b15
1.0.0.b16
1.0.0.b17
1.0.0.b18
1.0.0.b19
1.0.0.b20
1.0.0.b21
1.0.0.b22
now in linux with GNU sort it is easy - just use sort -V
But this has also to work on macOS where I do not have any experience on it, but from testing I know -V does not work there.
I tried with
sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n
but no luck there.
I want to have it sorted by Version/buildnumber, e.g.
1.1.3.b5 is higher than 1.0.3.b66
what have I missed here? Can you please help me? Also, unfortuneatly, installing homebrew coreutils are not an option
thank you,
br Alex
I assume your real full list won't have all b versions. You'll need to split field 4 into two keys; one for the alpha part and one for the numeric part.
$: sort -t. -k1n -k2n -k3n -k4.1,4.1 -k4.2n vnums
1.0.0.a5
1.0.0.a10
1.0.0.a13
1.0.0.a19
1.0.0.b1
1.0.0.b6
1.0.0.b8
1.0.0.b9
1.0.0.b12
1.0.0.b14
1.0.0.b17
1.0.0.b20
1.0.0.b21
1.0.0.b22
1.0.0.c3
1.0.0.c7
1.0.0.c15
1.0.0.c16
1.0.0.d2
1.0.0.d4
1.0.0.d11
1.0.0.d18
1.0.3.b66
1.1.3.b5
Note the limiting of the alpha column of field 4 to a single character.
Perl one-liner
Assuming that there is no more than 6 digit (or fixed size), sprintf "%06", $& will sort over numbers left padded with 0:
perl -e 'sub v{"#_"=~s/\d+/sprintf"%06d",$&/ger}print sort{v($a)cmp v($b)} <>' inputfile
Treat the forth key as composed of subfields:
sort -t. -k1n,1 -k2n,2 -k3n,3 -k4.1,4.1 -k4.2n

bash sorting numbers with decimals [duplicate]

This question already has answers here:
Sorting numbers with multiple decimals in bash
(4 answers)
Unix sort of version numbers
(7 answers)
Sort version strings on bash
(2 answers)
Closed 5 years ago.
I have a file like so:
1.1
3.2
1.2
1.10
I would like to sort the file so that it looks like so:
1.1
1.2
1.10
3.2
In other words, 1.10 is bigger than 1.2
I tried:
sort -nk 1,1 file
But I keep getting this, which is not what I want
1.1
1.10
1.2
3.2
Thanks
With GNU sort:
sort -t "." -n -k1,1 -k2,2 file
Output:
1.1
1.2
1.10
3.2
You may use the -V option.
sort -V numbers
However this option is only in GNU Coreutils and could be absent from other implementation.
See https://stackoverflow.com/a/35386002/1107536

Convert integers to floats in awk

cat file.txt
1
1.6
0
0.7
2
3
3.7
The expected output is:
1.0
1.6
0.0
0.7
2.0
3.0
3.7
any awk or bash solution? Wherever is integer, convert to float.
To awk, it is just a matter to indicate the format you want to use to print the results. Use printf to indicate that you want 1 decimal digit:
$ awk '{printf ("%.1f\n", $1)}' file
1.0
1.6
0.0
0.7
2.0
3.0
3.7
You can say %e for scientific format, %.5f to get 5 digits, etc. See the provided link for more details.
Similarly to the solution with awk, you can use printf with bash too, e.g. via xargs:
cat file.txt | xargs -n 1 printf "%.1f\n"
The formatting options of printf are similar.
Note that printf (either for awk or bash) will output rounded value. So if you have 0.77 in your input, the result of the %.1f format will be 0.8.

Sort CSV file based on first column

Is there a way to sort a csv file based on the 1st column using some shell command?
I have this huge file with more than 150k lines hence I can do it in excel:( is there an alternate way ?
sort -k1 -n -t, filename should do the trick.
-k1 sorts by column 1.
-n sorts numerically instead of lexicographically (so "11" will not come before "2,3...").
-t, sets the delimiter (what separates values in your file) to , since your file is comma-separated.
Using csvsort.
Install csvkit if not already installed.
brew install csvkit
Sort CSV by first column.
csvsort -c 1 original.csv > sorted.csv
I don't know why above solution was not working in my case.
15,5
17,2
18,6
19,4
8,25
8,90
9,47
9,49
10,67
10,90
13,96
159,9
however this command solved my problem.
sort -t"," -k1n,1 fileName

Resources