在UNIX中无法正常使用Sort命令对CSV文件进行排序 [英] Sort command in not working properly in unix for sorting a csv file

查看:125
本文介绍了在UNIX中无法正常使用Sort命令对CSV文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,需要根据时间戳进行排序.这是csv中的第三列,我正在使用以下命令进行排序:

I have a csv file which I need to order based on the timestamp. It is the third column in the csv and I am using below commands to sort:

awk 'NR<2{print $_;next}{ print $_ | "sort -t, -k3.8,3.11nr -k3.1,3.3rM -k3.4rd" }' 

当年份为单年时,此命令可以正确排序,但是对于存在多个年份的大数据,它会将较早的年份放在CSV的某个位置或中间.下面是一个示例:

This command is sorting properly when year is single, but for large data where the multiple years are present, it is putting the older ones first or in between somewhere of the csv. a sample is below:

data2,Send for Translation To CTM,Dec 30 2013 02:22
data1,Send for Translation To CTM,Dec 30 2013 02:20
data1,Send for Translation To CTM,Sep 30 2014 03:22
data2,Send for Translation To CTM,Oct 30 2014 03:21

我需要安排具有最新时间戳的数据,并且年份应该按以下顺序排列:2014、2013、2012等...

I need to arrange the data with latest timestamp and year should go down in this order: 2014, 2013, 2012 and so on...

我该如何实现?

推荐答案

以下内容应该有效

 awk 'NR<2{print $_;next}{ print $_ | "sort -t, -k3.8,3.11rn -k3.1,3.3rM -k3.5,3.6rn -k3.12rd" }'

"awk"代码段将除标头之外的所有行都传递给sort命令. 键的顺序在这里很重要:

The 'awk' snippet passes all lines except header to the sort command. The order of the keys is important here :

k3.8,3.11rn提取列的年份部分并进行反向排序

k3.8,3.11rn extracts the year part of the column and reverse sorts

k3.1,3.3rM提取第三列中的前3个字符以进行每月反向排序,其余的我们进行反向字典排序

k3.1,3.3rM extracts the first 3 characters in column three to be reverse monthly sorted and the rest we do a reverse dictionary sort

k3.5,3.6rn提取日期和反向排序,最后我们在时间上也做同样的事情

k3.5,3.6rn extracts the day and reverse sort and finally we do the same for time

这篇关于在UNIX中无法正常使用Sort命令对CSV文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆