Sorting a column of CSV file resulting in 1123 appearing before 232

Specific question - bad title I am trying to sort a set of CSV data like -

2014/06/04 13:19:06.970,1124,Post Request,200,OK,text,true,,782,1,0
2014/06/04 13:18:27.241,237,Post Request,200,OK,text,true,,303,1,0
2014/06/04 13:12:15.973,2224,Post Request,200,OK,text,true,,1551,1,0
2014/06/04 13:12:31.365,238,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:12:29.892,1429,Post Request,204,No Content,,true,,237,1,0

using -

sort -t"," -k2,3

The result is

2014/06/04 13:19:06.970,1124,Post Request,200,OK,text,true,,782,1,0
2014/06/04 13:18:27.241,1429,Post Request,200,OK,text,true,,303,1,0
2014/06/04 13:12:15.973,2224,Post Request,200,OK,text,true,,1551,1,0
2014/06/04 13:12:31.365,237,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:12:29.892,238,Post Request,204,No Content,,true,,237,1,0

But I want something like -

2014/06/04 13:19:06.970,237,Post Request,200,OK,text,true,,782,1,0
2014/06/04 13:18:27.241,238,Post Request,200,OK,text,true,,303,1,0
2014/06/04 13:12:15.973,1124,Post Request,200,OK,text,true,,1551,1,0
2014/06/04 13:12:31.365,1429,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:12:29.892,2224,Post Request,204,No Content,,true,,237,1,0

or reverse of this (actually reverse is better for me). What am I doing wrong?

Answers


Add -n to have it sorted numerically:

$ sort -n -t"," -k2,3 file
2014/06/04 13:18:27.241,237,Post Request,200,OK,text,true,,303,1,0
2014/06/04 13:12:31.365,238,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:19:06.970,1124,Post Request,200,OK,text,true,,782,1,0
2014/06/04 13:12:29.892,1429,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:12:15.973,2224,Post Request,200,OK,text,true,,1551,1,0

Or reverse with -r:

$ sort -rn -t"," -k2,3 file
2014/06/04 13:12:15.973,2224,Post Request,200,OK,text,true,,1551,1,0
2014/06/04 13:12:29.892,1429,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:19:06.970,1124,Post Request,200,OK,text,true,,782,1,0
2014/06/04 13:12:31.365,238,Post Request,204,No Content,,true,,237,1,0
2014/06/04 13:18:27.241,237,Post Request,200,OK,text,true,,303,1,0

From man sort:

-n, --numeric-sort

compare according to string numerical value

-r, --reverse

reverse the result of comparisons


Need Your Help

How to calculate the required memory for an assembly code?

assembly operating-system x86

This is a paragraph from broken thorn operating system development series. There are 3 lines of code. It will take 512 byte to load up to second line and 514 bytes up to third line. How can we calc...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.