0

I have a script that gets people to enter name, number and date in the format of YYYY/MM/DD which then appends to a file so it looks like:

Alex, 133454543, 1990/12/14
Tom, 234234235, 1992/10/09

I want to sort the file by the month and then day. I've tried using:

sort -k3,2

However neither of them have worked;

sort -k3

will sort by the year.

  • 2
    you should check out -t switch to define field separator. Also providing some test case where the sort would be noticeable would be nice. – Jakuje Jan 7 '16 at 16:23
  • 1
    Give larger input sample, and expected output. – jimmij Jan 7 '16 at 16:26
  • So with sort -t / -k 2,3 it should be working. – ott-- Jan 7 '16 at 16:28
  • 2
    Or just sort -t / -k 2 or sort -k 3.7 – Stéphane Chazelas Jan 7 '16 at 16:40
2

sort -t/ -k 2,3 will tell sort to use the second and third fields, delimited by /; I used this expanded input file:

$ cat file
Bob, 133454543, 1990/12/10
Alex, 133454543, 1990/12/11
Tom, 234234235, 1992/10/19
Zed, 234234235, 1992/10/18
$ sort -t/ -k 2,3 file
Zed, 234234235, 1992/10/18
Tom, 234234235, 1992/10/19
Bob, 133454543, 1990/12/10
Alex, 133454543, 1990/12/11

Note that this ignores the year field for sorting (as implied by the question).

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.