Experts,
I am new to scripting world
I am trying to keep the lines which are older than 30 days. the file contains following lines
Server1 last patched on Mon Oct 11 09:50:47 2021
Server2 last patched on Fri Jun 3 07:53:36 2022
Server3 last patched on Fri Jun 3 11:58:26 2022
Server4 last patched on Fri Jun 17 12:58:59 2022
Server5 last patched on Fri Marc 17 04:12:51 2022
Server6 last patched on Mon Oct 17 23:08:24 2022
Thank you for your help.
expecting to keep the lines which are older than 30 days. i have tried this
awk -v dat="Sun Oct 04 00:00:00 2022" -F':' '$5<dat' list.txt
gives same result
Server1 last patched on Mon Oct 11 09:50:47 2021
Server2 last patched on Fri Jun 3 07:53:36 2022
Server3 last patched on Fri Jun 3 11:58:26 2022
Server4 last patched on Fri Jun 17 12:58:59 2022
Server5 last patched on Fri Marc 17 04:12:51 2022
Server6 last patched on Mon Oct 17 23:08:24 2022
expected results is
Server1 last patched on Mon Oct 11 09:50:47 2021
Server2 last patched on Fri Jun 3 07:53:36 2022
Server3 last patched on Fri Jun 3 11:58:26 2022
Server4 last patched on Fri Jun 17 12:58:59 2022
Server5 last patched on Fri Marc 17 04:12:51 2022
CodePudding user response:
The difficulty is having to parse the timestamps into an actual time value: you can't just compare them as strings and expect chronological order.
Here's a bit of perl:
perl -MTime::Piece -lane '
BEGIN {$start = (localtime) - 86400 * 30}
$t = Time::Piece->strptime("@F[4..8]", "%a %b %d %T %Y");
print if $t < $start;
' file
Server1 last patched on Mon Oct 11 09:50:47 2021
Server2 last patched on Fri Jun 3 07:53:36 2022
Server3 last patched on Fri Jun 3 11:58:26 2022
Server4 last patched on Fri Jun 17 12:58:59 2022
Server5 last patched on Fri Mar 17 04:12:51 2022
Note, I had to edit Marc
to Mar
to satisfy strptime's %b
month abbreviation.