1

I have a file in the following format

222 aaaaa 2021-12-24
333fd bbb 2021-09-1
444 cc 2022-01-16
555 zzz 2021-08-21
6666 www 2021-09-5
777 yyy 2021-03-14
888 xxx 2021-11-27
999 ffffff 2021-12-4

I am trying to order the file based on the date column.

I have tried to sort :

  • with option -t "-" -k2 delimiter but doesn't work. (tried to order by month at least)
  • with option -k3.4 (it can order by the 4th char of the year 2021/2022), piping with | sort -n -k3.5 but doesn't work
xavi
  • 13

1 Answers1

1

YYYY-MM-DD can be sorted alphabetically, so the only thing you need is to add the leading zeroes to days.

sed -E 's/-(.)$/-0\1/' file | sort -k3

You can add | sed -E 's/-0(.)$/-\1/' to get back the original format (but why?).

choroba
  • 20,299