Sort command in not working properly in unix for sorting a csv file

后端 未结 3 2018
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 02:07

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 $_;n         


        
相关标签:
3条回答
  • 2021-01-27 02:17

    Try this:

    sort -rft',' -k3 merged.csv
    
    0 讨论(0)
  • 2021-01-27 02:26

    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

    0 讨论(0)
  • 2021-01-27 02:31

    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. :)

    0 讨论(0)
提交回复
热议问题