Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Use sort to sort numerical column


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Use sort to sort numerical column

How to sort the following output based on lowest to highest BE?
The following sort does not work.

Code:
$ sort -t. -k1,1n -k2,2n bfd.txt

 BE31.116       0s               0s               DOWN DAMP
 BE31.116       0s               0s               DOWN DAMP
BE31.117       0s               0s               DOWN DAMP
 BE31.702       0s               0s               DOWN DAMP
 BE31.961       0s               0s               DOWN DAMP
 BE62.2040      0s               0s               DOWN DAMP  <---- should be last
 BE31.3918      0s               0s               DOWN DAMP

Thanks.
# 2  
Hi, try so
Code:
sort -k1.3,1 bfd.txt

--- Post updated at 18:34 ---

Code:
sort -V bfd.txt

--- Post updated at 18:42 ---

Code:
sort -t. -k1.3,1.4n -k2,2n bfd.txt

# 3  
Try:
Code:
sed 's/^ *//' file | sort -k1.3,1n


Last edited by Scrutinizer; 05-20-2019 at 02:14 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Thanks Scrutinizer
@sand1234, If there are leading spaces the following command gives the order as you showed above.
Code:
sort -bV bfd.txt

This User Gave Thanks to nezabudka For This Post:
# 5  
Thanks nezabudka,

Indeed -b can be used to ignore leading blanks; the sed statement can be used to remove leading spaces and correct the output..

The -V option is a non-standard extension that is carried by GNU Sort and BSD Sort and is used when the dot in the number signifies a major.minor version number. The -n option is used for numerical sorts.

They give different results, depending on the meaning of the dotted number, the OP has not indicated what is the case..

S.
This User Gave Thanks to Scrutinizer For This Post:
# 6  
reviewing solutions

reviewing solutions

--- Post updated at 12:38 PM ---

Hi,

1st solution works:

Code:
$ sed 's/^ *//' bfd.txt | sort -bV

BE31.116              0s               0s               DOWN DAMP
BE31.116               0s               0s               DOWN DAMP
BE31.117              0s               0s               DOWN DAMP
BE31.702                 0s               0s               DOWN DAMP
BE31.961              0s               0s               DOWN DAMP
BE31.3918           0s               0s               DOWN DAMP
BE62.2040               0s               0s               DOWN DAMP

2nd solution fails:

Code:
$ sed 's/^ *//' bfd.txt | sort -k1.3,1n

BE31.116              0s               0s               DOWN DAMP
BE31.116              0s               0s               DOWN DAMP
BE31.117               0s               0s               DOWN DAMP
BE31.3918             0s               0s               DOWN DAMP  <----- incorrect
BE31.702                 0s               0s               DOWN DAMP
BE31.961            0s               0s               DOWN DAMP
BE62.2040               0s               0s               DOWN DAMP

Can you please shed some light on why the 2nd solution does not work?
Also, why do we need 1n after -k1,3? I get the same result with and without this switch.

Thanks,

Last edited by Scrutinizer; 05-21-2019 at 11:52 AM..
# 7  
See post #5. It is the difference between numerical and version number sort..

With version numbers 1.11 comes after 1.9. With numerical sort 1..11 is smaller than than 1.9 .

You had not specified which case applies..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Gawk: PROCINFO["sorted_in"] does not sort my numerical array values
beca123456
Hi, PROCINFO seems to be a great function but I don't manage to make it works. input: B,A,C B B,B As an example, just want to count the occurence of each letter across the input and sort them by decreased order. Wanted output: B 4 A 1 C 1 When I use this command, the PROCINFO...... UNIX for Beginners Questions & Answers
4
UNIX for Beginners Questions & Answers
How to sort a column based on numerical ascending order if it includes e-10?
evelibertine
I have a column of numbers in the following format: 1.722e-05 2.018e-05 2.548e-05 2.747e-05 7.897e-05 4.016e-05 4.613e-05 4.613e-05 5.151e-05 5.151e-05 5.151e-05 6.1e-05 6.254e-05 7.04e-05 7.12e-05 7.12e-05... UNIX for Dummies Questions & Answers
6
UNIX for Dummies Questions & Answers
Sort numerically a non numerical
gio001
Hello, I have this sample data: 01 * * * * 01 * * * * 01 * * * * 01 * * * * 01 0 * * * 01 0 * * * 01 0 * * * 01 0 * * * 02 * * * 0 02 * * * 0 02 * * * 6 02 * * * 6 02 0 * * 1 02 0 * * 1 02 0 * * 2 02 0 * * 2 02 0 * * 3... Shell Programming and Scripting
3
Shell Programming and Scripting
script to sort a string of numerical data in set fields
tlarkin
So, I will be working with someone and basically we are trying to build a form that is submitted most likely via the web and the data is just a string of numbers. like: 19383882872201110929282821818182827349190102837364718191001932873711 Now, each number is part of a numerical value of...... Shell Programming and Scripting
4
Shell Programming and Scripting

Featured Tech Videos