안녕하세요. 인증서 만료 날짜를 사용하여 아래 파일을 정렬하려면 어떻게 해야 하나요? 감사합니다.
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