-2

Hi How can i sort below file so as to have cert expiration date is used to sort this, thanks

Cluster10*  expire date: Feb 22 23:59:59 2024 GMT
Cluster11*  expire date: Feb  1 23:59:59 2023 GMT
Cluster23*  expire date: Jan 24 23:59:59 2021 GMT
Cluster24*  expire date: May 23 23:59:59 2023 GMT
Ed Morton
  • 28,789
  • 5
  • 20
  • 47

3 Answers3

3

Decorate-sort-undecorate:

$ awk -v mths='JanFebMarAprMayJunJulAugSepOctNovDec' '
    { printf "%d%02d%02d%s\t%s\n", $7, (index(mths,$4)+2)/3, $5, $7, $0 }
' file | sort | cut -f2-
Cluster23*  expire date: Jan 24 23:59:59 2021 GMT
Cluster11*  expire date: Feb  1 23:59:59 2023 GMT
Cluster24*  expire date: May 23 23:59:59 2023 GMT
Cluster10*  expire date: Feb 22 23:59:59 2024 GMT
Ed Morton
  • 28,789
  • 5
  • 20
  • 47
1

I'd convert the dates to unixtime and then sort:

$ cat test.txt | while read line; do echo -n "$line|"; date -d "`echo "$line" | cut -c 26-49`" +%s; done | sort -t\| -k2
Cluster23*  expire date: Jan 24 23:59:59 2021 GMT|1611532799
Cluster11*  expire date: Feb  1 23:59:59 2023 GMT|1675295999
Cluster24*  expire date: May 23 23:59:59 2023 GMT|1684886399
Cluster10*  expire date: Feb 22 23:59:59 2024 GMT|1708646399
Rob Bos
  • 4,250
  • 1
  • 11
  • 12
  • 1
    If you copy/paste that code into http://shellcheck.net it'll tell you of some issues with it and [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) will tell you about others. – Ed Morton Jul 19 '22 at 21:11
  • Thanks all, i tested above & worked for me. – Kiran Kumar Jul 20 '22 at 06:50
  • 1
    @EdMorton Good feedback, thanks. – Rob Bos Jul 21 '22 at 16:21
1

and Time::Piece module:

perl -MTime::Piece -nle '
    push @A, $_ ;}{
    print for(sort {
        Time::Piece->strptime(join(" ", (split /  ?/, $a)[3..7]), "%b %d %T %Y %Z")
            <=>
        Time::Piece->strptime(join(" ", (split /  ?/, $b)[3..7]), "%b %d %T %Y %Z")
    } @A)
' sample
DanieleGrassini
  • 2,769
  • 5
  • 17