1

I have this data to sort. The 1st column is the item ID. The 2nd column is the numerical value. Some items do not have a numerical value.

03875334    -4.27
03860156    -7.27
03830332    
19594535    7.87
01542392    -5.74
01481815    11.45
04213946    -10.06
03812865    -8.67
03831625    
01552174    -9.28
13540266    -8.27
03927870    -7.25
00968327    -8.09

I want to use the Linux sort command to sort the items numerically in the ascending order of their value, but leave those empty items to the end. So, this is the expected output I want to obtain:

04213946    -10.06
01552174    -9.28
03812865    -8.67
13540266    -8.27
00968327    -8.09
03860156    -7.27
03927870    -7.25
01542392    -5.74
03875334    -4.27
19594535    7.87
01481815    11.45
03830332    
03831625    

I tried "sort -k2n" and "sort -k2g", but neither yielded the output I want. Any idea?

Jacky Lee
  • 1,193
  • 3
  • 13
  • 22

3 Answers3

2

Assuming data is in d.txt and blanks have 4 spaces at the end

egrep "    $" d.txt > blanks.txt ; egrep -v "    $" d.txt | sort -n -k2 | cat - blanks.txt
Sameer Naik
  • 1,326
  • 1
  • 13
  • 28
  • 1
    The `&&` will cause the command to fail with an empty output file of there are no empty cells. You want to change it to a plain `;` for robustness. – tripleee Jan 19 '15 at 06:03
2

Here is a simple Schwartzian transform based on the assumption that all actual values are smaller than 123456789.

awk '{ printf "%s\t%s", ($2 || 123456789), $0 }' file |
sort -n | cut -f2- >output
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Inspired by tripleee's answer, I think this gives the best solution: awk '{ printf "%s\t%s\n", ($2 || 0), $0 }' log.csv | sort -k1nr -k3n | cut -f2- – Jacky Lee Jan 19 '15 at 07:41
0

This should work:

awk '$2 ~ /[0-9]$/' d.txt | sort  -k2g && awk '$2 !~ /[0-9]$/' d.txt
vmcloud
  • 650
  • 4
  • 13