0

How to sort text file by date properly in sample below? The problem is that sort -k1,1 does not really work and neither can i use the dot notation such as sort -k1.6,1.9 -k1.4,1.4 -k1.1,1.2 because the form does not have a fixed length. I came up with this horror

awk '{split($1,a,".");print a[1],a[2],a[3],$2}' test.txt | sort -k3,3 -k2,2 -k1,1 | awk '{print  $1 "." $2 "." $3,$4}'

while it works, i don't like the complexity. Can this be simplified, ideally with just sort?

25.3.2020   26698
24.3.2020   22600
23.3.2020   19624
22.3.2020   17377
21.3.2020   15584
20.3.2020   13704
4.3.2020    407
3.3.2020    340
2.3.2020    262
1.3.2020    211
29.2.2020   200
28.2.2020   193
27.2.2020   170
26.2.2020   135
11.2.2020   74
10.2.2020   72
9.2.2020    64
8.2.2020    62
7.2.2020    62
6.2.2020    56
5.2.2020    53
2.2.2020    43
1.2.2020    38
31.1.2020   37
30.1.2020   34
28.1.2020   28
27.1.2020   20
  • test.txt contains like it: 22.3.2020 17377? – 69 420 1970 Mar 26 at 21:36
  • @d1553ct10n yes, the test.txt contains the data that are pasted above. – leosenko Mar 26 at 21:41
  • @guest Yes it does – bashBedlam Mar 26 at 23:11
1

Try:

sort -n -t. -k3,3 -k2,2 -k1,1 test.txt
  • -n sort numerically
  • -t . use dot character as field separator
| improve this answer | |

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.