0

I have data in my file like following

07020,6880A98805,03/09/2014
04374,6880698805,09/01/2014
05019,6880698805,07/01/2014
05020,2710239905,08/02/2007
05020,2710239905,08/31/2007
05021,2710239905,09/01/2007
05806,2710239905,08/30/2007
05807,8803975905,08/01/2014
05808,8803975905,06/01/2014
06019,6880698805,07/01/2006
06020,6880698805,03/09/2014
07807,8803975905,08/01/2014
13375,3470566899,07/16/2015
13376,3470566899,08/16/2016
058030,6880698805,01/12/201
058030,6880698805,01/12/2015
058030,6880698805,01/12/2015
100004,8803975905,03/09/2014
100005,8803975905,01/01/2015
100006,8803975905,02/01/2015
100007,8626833130,01/16/2016
100031,8803975905,01/12/2015
100031,8803975905,01/12/2015
100031,8803975905,01/12/2015
100032,8803975905,01/01/2015

I want to sort the file order by first field (numerically) and then second field (numerically) and then third.

Using the forums I tried several but somehow I am not getting the required order by using:

sort -n -t',' -k1.1n,1.15n -k2.1n,2.10n -k3.7n,3.10n -k3.1n,3.2n -k3.4n,3.5n <file_name.csv>

Can someone let me know, what is going wrong?

  • You have an A in the second field, so it's no longer numeric, which in turn means you shouldn't be using -n as part of the sort criteria for field 2. – roaima Dec 22 '16 at 23:40
  • There is a possibility that it can contain alphabets,but still i want the data to be sorted by numeric order(like order by in database).. Even If i am using sort -n -t',' -k1.1n ,1.15n <file_name.csv> , this also not working as expected – user206852 Dec 22 '16 at 23:47
  • 1
    You cannot (by definition) sort alphanumeric data numerically – roaima Dec 22 '16 at 23:47
1

It looks like you were pretty close. You can't sort the second field numerically, though, because it's not entirely numeric (there's an A in the first line).

This works for me:

sort -t, -k1,1n -k2,2 -k3.7,3.10 -k3.1,3.2n -k3.4,3.5n
  • Thanks roaima, seems this works for me..Let me work on some more sample data – user206852 Dec 23 '16 at 0:12
  • @user206852 Please remember to accept this answer if it's the one that works best for you. That's how StackExchange sites work. – roaima Dec 23 '16 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.