1

I have a large number of CSV files, each file having multiple numeric columns. The first column is Unix timestamp. How can I sort such file? Thank you.

An example row of the file as below:

1376317246; 4;  11703.99824;    10477.029091173334; 89.51666666666667;  6.7108864E7;    2.0937962933333334E7;   0.0;    1.4;    235.53333333333333; 3.8666666666666667
1

I wouldn't call it a CSV file if it looks like that.

sort -t";" filename

should work. This will sort on all columns. If that's unappealing, then this should suit you.

From the docs:

   -t, --field-separator=SEP
          use SEP instead of non-blank to blank transition
3
  • Specifying the delimiter is of no use here as you don't use it. That would be useful in sort -t ';' -k2,2 to sort based on the second field. But not here we're you're sorting based on the full content of the line. – Stéphane Chazelas Mar 22 '19 at 14:42
  • Although I missed that, @StéphaneChazelas It's still necessary because ; is larger than digits. So if there's a row whose first field is 137631724 (this is the value in the example without the trailing 6), sorting on the whole content will place this below the 1376317246, which would be incorrect. – y_wc Mar 22 '19 at 15:07
  • Still, -t will make no difference. You'd need sort -t ';' -k1,1 for it to make a difference. But here, all you need and want is sort -n. – Stéphane Chazelas Sep 30 '19 at 13:37
1

you could use sort

sort --field-separator=';' --key=1 yourCSVfile.csv 

if you need to create a new sorted file just add an output file:

sort --field-separator=';' --key=1 yourCSVfile.csv > sortedCSVfile.csv
1

I would use sort with following option:

  • -g (--general-numeric-sort) for numeric sort just in case, because default sorting mechanism is alphanimeric and sorting {9..11} would result in 10 11 9.
sort -g filename
1
  • you're right. I'll edit the answer. So -g would be sufficient. – Jakub Jindra Mar 22 '19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.