Sorting a column of CSV file resulting in 1123 app

2019-09-16 04:43发布

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?

1条回答
贼婆χ
2楼-- · 2019-09-16 05:23

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

查看更多
登录 后发表回答