-2

how do I sort lists like the one below using sort or any related commands?

12.ale.panal.sel.blr.teta.hf.ew.rr.lwq.ors.2018
34.ev.we.ars.lmn.2017
5.lam.bere.meto.belagn.hede.we.e.2020
54.arad.met.kal.sil.tek.br.yz.2005

I want to sort them according to the last word which is the year chronologically?

N.B basically something like sort -t. -k-1 although -1 is not allowed. Any command just to address the last part?

6
  • the third word meaning the word after 3 dots from the start of the line
    – Sol
    Jun 8, 2020 at 22:52
  • not really because they are not columns rather just words and numbers just connected with dots
    – Sol
    Jun 8, 2020 at 22:54
  • 1
    You just have to tell sort that your field separator is the dot. Check the -t flag. Jun 8, 2020 at 22:56
  • 2
    You know, one of the conditions of asking here is to do some research first. Sometimes people will give you an answer, but usually you have to show that you've tried. The command sort sorts based on fields (see the answer linked above). By default the field separator is whitespace, with -t. the field separator will be a dot. You' most likely can pick it up from there. Jun 8, 2020 at 23:00
  • Yes, using both flags. Jun 8, 2020 at 23:07

1 Answer 1

1

With a Schwartzian transform

... | awk -F. -v OFS=. '{print $NF, $0}' | sort -t. -n -k1,1 | cut -d. -f2-

where ... is whatever command you use to produce the list.

0

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