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
Related
This question already has answers here:
How to write integer to binary file using Bash? [duplicate]
(3 answers)
Closed 3 years ago.
I need to write the size of a file in a binary file using bash. But I want it to stay on 4 bytes:
The length of the file is 224128 bytes. So if I do
echo -n -e '224128' > output.bin
it will compute 6 bytes, but I want it to occupy 4 bytes. How can I do?
I don't know if I made myself clear.
Thank you very much in advance!
Perl to the rescue:
perl -we 'print pack "N", shift' 224128
To check the output, you can use e.g. xxd:
$ perl -we 'print pack "N", shift' 224128 | xxd
00000000: 0003 6b80 ..k.
And indeed,
$ echo $((16#36b80))
224128
If you need different endianness, check pack.
This question already has answers here:
How to compare two strings in dot separated version format in Bash?
(38 answers)
Closed 3 years ago.
I want to check gcc version whether >= 9.1.
First, I type this shell:
$ gcc --version | grep gcc | awk '{print $3}'
I get following in my computer after I type above shell:
9.1.0
I know bash only can compare number of integer,
such as command "-gt".
I want to compare gcc which version >= 9.1 .
How do I do?
You need not to use grep with awk, awk could look for string and could do comparison too. Could you please try following.
gcc --version | awk '/gcc/ && ($3+0)>9.1{print "Version is greater than 9.1"}'
This question already has answers here:
Linux shell sort file according to the second column?
(4 answers)
Closed 8 years ago.
I have a simple data set. example:
IP address,number of times it appears
192.168.0.10,11
192.168.0.1,15
192.168.0.120,9
I want to use the sort command to sort these by the largest number of times the IP has been seen. the output should look like:
192.168.0.1,15
192.168.0.10,11
192.168.0.120,9
Use -k to specify the column. Don't forget about -n to use numeric ordering.
sort -n -k2 file.txt
This question already has answers here:
How to sort strings that contain a common prefix and suffix numerically from Bash?
(5 answers)
Closed 6 years ago.
I've often wanted to sort strings with numbers in them so that, when sorting e.g. abc_2, abc_1, abc_10 the result is abc_1, abc_2, abc_10. Every sort mechanism I've seen sorts as abc_1, abc_10, abc_2, that is character by character from the left.
Is there any efficient way to sort to get the result I want? The idea of looking at every character, determining if it's a numeral, building a substring out of subsequent numerals and sorting on that as a number is too appalling to contemplate in bash.
Has no bearded *nix guru implemented an alternative version of sort with a --sensible_numerical option?
Execute this
sort -t _ -k 2 -g data.file
-t separator
-k key/column
-g general numeric sort
I think this is a GNU extension to sort, but you're looking for the --version-sort (or -V) option:
$ printf "prefix%d\n" $(seq 10 -3 1)
prefix10
prefix7
prefix4
prefix1
$ printf "prefix%d\n" $(seq 10 -3 1) | sort
prefix1
prefix10
prefix4
prefix7
$ printf "prefix%d\n" $(seq 10 -3 1) | sort --version-sort
prefix1
prefix4
prefix7
prefix10
https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
You can sort using version-sort
Just pass the following arg
-V or --version-sort
# without (ersion-sort)
$ cat a.txt
abc_1
abc_4
abc_2
abc_10
abc_5
# with (version-sort)
$ sort -V a.txt
abc_1
abc_2
abc_4
abc_5
abc_10
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