对CSV文件列进行排序,导致1123出现在232之前

时间:2014-06-04 08:00:42

标签: shell sorting csv man

具体问题 - 糟糕的标题 我正在尝试对一组CSV数据进行排序,例如 -

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

使用 -

sort -t"," -k2,3

结果是

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

但我想要像 -

这样的东西
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

或反向(实际上反向对我来说更好)。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

添加-n以使其按数字排序:

$ 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

或以-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

来自man sort

  

-n , - 数字排序

     

根据字符串数值进行比较

     

-r , - 反向

     

反转比较结果