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" }'
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
I need to arrange the data with latest timestamp and year should go down in this order: 2014, 2013, 2012 and so on...
How can I acheive this?
尝试这个:
sort -rft',' -k3 merged.csv
I will try to sort by date and then by time
awk -F"," '{print $3,$1,$2}' file.csv | sort -d' ' -k 1d -k 2d
BTW, it would be great if you just share a small section of your file. :)
The below should work
awk 'NR<2{print $_;next}{ print $_ | "sort -t, -k3.8,3.11rn -k3.1,3.3rM -k3.5,3.6rn -k3.12rd" }'
The 'awk' snippet passes all lines except header to the sort command. The order of the keys is important here :
k3.8,3.11rn
extracts the year part of the column and reverse sorts
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
extracts the day and reverse sort and finally we do the same for time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.