こんにちは。証明書の有効期限を使用して以下のファイルを並べ替えるにはどうすればよいですか?ありがとうございます。
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
答え1
装飾 - 並べ替え - 装飾解除:
$ 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
答え2
日付をUnixtimeに変換してソートします。
$ 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
答え3
真珠そしてTime::Piece
モジュール:
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