您好,我如何对下面的文件进行排序,以便使用证书到期日期来对此进行排序,谢谢
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