简体   繁体   中英

Sort real numbers with sort in unix

I have txt file with this format str str real real real and i want to sort it based on the last column. I typed:

 sort -s -g -k 5 

but the file is not sorted with the 5th column. Ie the first lines after sorting look like:

[ 40.7243 -74.002 ] [ 40.6456 -73.7764] 20.9447883036 20.962214249 0.017425945409
[ 40.7566 -73.846 ] [ 40.7213 -73.9889] 12.6667858845 12.6773281976 0.0105423130825
[ 39.9815 -74.129 ] [ 44.2278 -76.48  ] 510.533346696 510.821713287 0.288366590938
[ 44.2278 -76.48  ] [ 40.6823 -73.9963] 443.855375141 444.135078426 0.279703285008
[ 39.7813 -75.121 ] [ 40.9335 -73.9   ] 164.718235848 164.850765753 0.132529904956
[ 40.9335 -73.9   ] [ 40.7459 -74.2606] 36.8255062031 36.8561101253 0.0306039222244
[ 39.9553 -75.156 ] [ 41.8296 -72.4828] 306.529624263 306.755220276 0.225596012547
[ 39.3533 -74.445 ] [ 40.7896 -74.466 ] 159.764267824 159.893075536 0.128807712368
[ 40.7896 -74.466 ] [ 40.6897 -74.1794] 26.5863055084 26.6084178441 0.022112335672
[ 40.7734 -73.871 ] [ 40.6726 -74.0066] 16.0110921238 16.0244162595 0.0133241356634
[ 40.7734 -73.871 ] [ 40.7404 -73.9843] 10.2271443583 10.235656793 0.00851243471041
[ 40.3169 -75.131 ] [ 43.1779 -73.0528] 361.914932074 362.167532812 0.252600737907
[ 40.7518 -73.977 ] [ 40.7029 -73.9905] 5.55672319197 5.56134868107 0.00462548910303
[ 40.7439 -73.984 ] [ 40.7259 -73.9948] 2.19927425701 2.20110502049 0.00183076347611
[ 40.7452 -73.977 ] [ 43.042  -76.1361] 311.772529032 312.000945947 0.228416914742
[ 40.7221 -73.95  ] [ 39.8748 -74.9226] 125.253302286 125.355551049 0.102248762774
[ 60.1695  24.95  ] [ 40.6807 -73.9778] 6622.44823647 6333.7345939 288.713642571
[ 39.9414 -75.026 ] [ 40.7405 -74.0059] 124.009549001 124.11082192 0.10127291917
[  37.5233 -122.254 ] [ 40.7488 -74.0235] 4124.03412435 4055.82258363 68.2115407237
[ 39.8492 -74.973 ] [ 40.6897 -74.1794] 115.218817935 115.313160665 0.0943427295254

What if you print the last column, sort the output and finally remove that column?

awk '{print $NF, $0}' file | sort -s -g | cut -d' ' -f2-

By pieces:

Print the last column in the beginning of the line:

$ awk '{print $NF, $0}' file
0.017425945409 [ 40.7243 -74.002 ] [ 40.6456 -73.7764] 20.9447883036 20.962214249 0.017425945409
0.0105423130825 [ 40.7566 -73.846 ] [ 40.7213 -73.9889] 12.6667858845 12.6773281976 0.0105423130825
0.288366590938 [ 39.9815 -74.129 ] [ 44.2278 -76.48  ] 510.533346696 510.821713287 0.288366590938
0.279703285008 [ 44.2278 -76.48  ] [ 40.6823 -73.9963] 443.855375141 444.135078426 0.279703285008
0.132529904956 [ 39.7813 -75.121 ] [ 40.9335 -73.9   ] 164.718235848 164.850765753 0.132529904956
0.0306039222244 [ 40.9335 -73.9   ] [ 40.7459 -74.2606] 36.8255062031 36.8561101253 0.0306039222244
0.225596012547 [ 39.9553 -75.156 ] [ 41.8296 -72.4828] 306.529624263 306.755220276 0.225596012547
0.128807712368 [ 39.3533 -74.445 ] [ 40.7896 -74.466 ] 159.764267824 159.893075536 0.128807712368
0.022112335672 [ 40.7896 -74.466 ] [ 40.6897 -74.1794] 26.5863055084 26.6084178441 0.022112335672
0.0133241356634 [ 40.7734 -73.871 ] [ 40.6726 -74.0066] 16.0110921238 16.0244162595 0.0133241356634
0.00851243471041 [ 40.7734 -73.871 ] [ 40.7404 -73.9843] 10.2271443583 10.235656793 0.00851243471041
0.252600737907 [ 40.3169 -75.131 ] [ 43.1779 -73.0528] 361.914932074 362.167532812 0.252600737907
0.00462548910303 [ 40.7518 -73.977 ] [ 40.7029 -73.9905] 5.55672319197 5.56134868107 0.00462548910303
0.00183076347611 [ 40.7439 -73.984 ] [ 40.7259 -73.9948] 2.19927425701 2.20110502049 0.00183076347611
0.228416914742 [ 40.7452 -73.977 ] [ 43.042  -76.1361] 311.772529032 312.000945947 0.228416914742
0.102248762774 [ 40.7221 -73.95  ] [ 39.8748 -74.9226] 125.253302286 125.355551049 0.102248762774
288.713642571 [ 60.1695  24.95  ] [ 40.6807 -73.9778] 6622.44823647 6333.7345939 288.713642571
0.10127291917 [ 39.9414 -75.026 ] [ 40.7405 -74.0059] 124.009549001 124.11082192 0.10127291917
68.2115407237 [  37.5233 -122.254 ] [ 40.7488 -74.0235] 4124.03412435 4055.82258363 68.2115407237
0.0943427295254 [ 39.8492 -74.973 ] [ 40.6897 -74.1794] 115.218817935 115.313160665 0.0943427295254

Sort with your current options:

$ awk '{print $NF, $0}' a | sort -s -g 
0.00183076347611 [ 40.7439 -73.984 ] [ 40.7259 -73.9948] 2.19927425701 2.20110502049 0.00183076347611
0.00462548910303 [ 40.7518 -73.977 ] [ 40.7029 -73.9905] 5.55672319197 5.56134868107 0.00462548910303
0.00851243471041 [ 40.7734 -73.871 ] [ 40.7404 -73.9843] 10.2271443583 10.235656793 0.00851243471041
0.0105423130825 [ 40.7566 -73.846 ] [ 40.7213 -73.9889] 12.6667858845 12.6773281976 0.0105423130825
0.0133241356634 [ 40.7734 -73.871 ] [ 40.6726 -74.0066] 16.0110921238 16.0244162595 0.0133241356634
0.017425945409 [ 40.7243 -74.002 ] [ 40.6456 -73.7764] 20.9447883036 20.962214249 0.017425945409
0.022112335672 [ 40.7896 -74.466 ] [ 40.6897 -74.1794] 26.5863055084 26.6084178441 0.022112335672
0.0306039222244 [ 40.9335 -73.9   ] [ 40.7459 -74.2606] 36.8255062031 36.8561101253 0.0306039222244
0.0943427295254 [ 39.8492 -74.973 ] [ 40.6897 -74.1794] 115.218817935 115.313160665 0.0943427295254
0.10127291917 [ 39.9414 -75.026 ] [ 40.7405 -74.0059] 124.009549001 124.11082192 0.10127291917
0.102248762774 [ 40.7221 -73.95  ] [ 39.8748 -74.9226] 125.253302286 125.355551049 0.102248762774
0.128807712368 [ 39.3533 -74.445 ] [ 40.7896 -74.466 ] 159.764267824 159.893075536 0.128807712368
0.132529904956 [ 39.7813 -75.121 ] [ 40.9335 -73.9   ] 164.718235848 164.850765753 0.132529904956
0.225596012547 [ 39.9553 -75.156 ] [ 41.8296 -72.4828] 306.529624263 306.755220276 0.225596012547
0.228416914742 [ 40.7452 -73.977 ] [ 43.042  -76.1361] 311.772529032 312.000945947 0.228416914742
0.252600737907 [ 40.3169 -75.131 ] [ 43.1779 -73.0528] 361.914932074 362.167532812 0.252600737907
0.279703285008 [ 44.2278 -76.48  ] [ 40.6823 -73.9963] 443.855375141 444.135078426 0.279703285008
0.288366590938 [ 39.9815 -74.129 ] [ 44.2278 -76.48  ] 510.533346696 510.821713287 0.288366590938
68.2115407237 [  37.5233 -122.254 ] [ 40.7488 -74.0235] 4124.03412435 4055.82258363 68.2115407237
288.713642571 [ 60.1695  24.95  ] [ 40.6807 -73.9778] 6622.44823647 6333.7345939 288.713642571

Remove the column:

$ awk '{print $NF, $0}' file | sort -s -g | cut -d' ' -f2-
[ 40.7439 -73.984 ] [ 40.7259 -73.9948] 2.19927425701 2.20110502049 0.00183076347611
[ 40.7518 -73.977 ] [ 40.7029 -73.9905] 5.55672319197 5.56134868107 0.00462548910303
[ 40.7734 -73.871 ] [ 40.7404 -73.9843] 10.2271443583 10.235656793 0.00851243471041
[ 40.7566 -73.846 ] [ 40.7213 -73.9889] 12.6667858845 12.6773281976 0.0105423130825
[ 40.7734 -73.871 ] [ 40.6726 -74.0066] 16.0110921238 16.0244162595 0.0133241356634
[ 40.7243 -74.002 ] [ 40.6456 -73.7764] 20.9447883036 20.962214249 0.017425945409
[ 40.7896 -74.466 ] [ 40.6897 -74.1794] 26.5863055084 26.6084178441 0.022112335672
[ 40.9335 -73.9   ] [ 40.7459 -74.2606] 36.8255062031 36.8561101253 0.0306039222244
[ 39.8492 -74.973 ] [ 40.6897 -74.1794] 115.218817935 115.313160665 0.0943427295254
[ 39.9414 -75.026 ] [ 40.7405 -74.0059] 124.009549001 124.11082192 0.10127291917
[ 40.7221 -73.95  ] [ 39.8748 -74.9226] 125.253302286 125.355551049 0.102248762774
[ 39.3533 -74.445 ] [ 40.7896 -74.466 ] 159.764267824 159.893075536 0.128807712368
[ 39.7813 -75.121 ] [ 40.9335 -73.9   ] 164.718235848 164.850765753 0.132529904956
[ 39.9553 -75.156 ] [ 41.8296 -72.4828] 306.529624263 306.755220276 0.225596012547
[ 40.7452 -73.977 ] [ 43.042  -76.1361] 311.772529032 312.000945947 0.228416914742
[ 40.3169 -75.131 ] [ 43.1779 -73.0528] 361.914932074 362.167532812 0.252600737907
[ 44.2278 -76.48  ] [ 40.6823 -73.9963] 443.855375141 444.135078426 0.279703285008
[ 39.9815 -74.129 ] [ 44.2278 -76.48  ] 510.533346696 510.821713287 0.288366590938
[  37.5233 -122.254 ] [ 40.7488 -74.0235] 4124.03412435 4055.82258363 68.2115407237
[ 60.1695  24.95  ] [ 40.6807 -73.9778] 6622.44823647 6333.7345939 288.713642571

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM