edited
I find myself frequently looking to see if something has stopped happening. To do this, it helps to see events in chronological order...
This solution seems to work, but the formatting still drives me insane...
The solution I have "sort of" working -
kubectl get events |
sed -E '/^[6789][0-9]s/{h; s/^(.).*/\1/; y/6789/0123/; s/^(.)/01m\1/;
x; s/^.(.*)/\1/; H;
x; s/\n//; };
s/^10([0-9]s)/01m4\1/; s/^11([0-9]s)/01m5\1/; s/^([0-9]s)/00m0\1/; s/^([0-9]+s)/00m\1/;
s/^([0-9]m)/0\1/; s/^([0-9]+m)([0-9]s)/\10\2/;
s/^L/_L/;' | sort -r
...this seems a bit like overkill to me.
The whitespace-delimited left-justified fields have no leading zeroes, report only seconds up to 2m as [0-9]+s
, then report as [0-9]+m[0-9]+s
up to 5m, after which it seems to report only [0-9]+m
.
Anyone have a short, maybe even simple-ish, easier to read solution that works?
No preference of tool (sed
, awk
, perl
, native bash
, etc), as long as it works and is likely to be already installed anywhere I need to work.
It's not a high priority, but seemed like a fun little challenge I thought I'd share.
My test data:
$: cat sample
LAST ...
28s ...
2m22s ...
46m ...
7s ...
75s ...
119s ...
Result with desired output -
$: sed -E '/^[6789][0-9]s/{h; s/^(.).*/\1/; y/6789/0123/; s/^(.)/01m\1/;
x; s/^.(.*)/\1/; H;
x; s/\n//; };
s/^10([0-9]s)/01m4\1/; s/^11([0-9]s)/01m5\1/; s/^([0-9]s)/00m0\1/; s/^([0-9]+s)/00m\1/;
s/^([0-9]m)/0\1/; s/^([0-9]+m)([0-9]s)/\10\2/;
s/^L/_L/;' sample | sort -r
_LAST ...
46m ...
02m22s ...
01m59s ...
01m15s ...
00m28s ...
00m07s ...
I've arbitrarily converted to a standardized version of the existing general output format just to keep it easily transferable to other members of the team. Either way, it's only being used for "eyeballing" the data, so other formats are not a problem as long as it's easy to read.
While there could theoretically include hours and days, such old events are usually not reported by this tool and are out of scope for this problem, and if needed I can likely extrapolate whatever solutions are presented. since I can get the order from this approach I'm really only looking for elegant formatting options.
A clumsy adaptation of Daweo's awk
solution with formatting -
$: awk '/^[0-9]/{ if($1!~/m/){$1="0m" $1}; split($1,arr,/m/);
t=arr[1]*60+arr[2]; m=(t-(t%60))/60; s=t-(m*60);
m=sprintf("%02dm",m); if(s){ s=sprintf("%02ds",s) } else s="";
$1=sprintf("%s%s",m,s); print; } /^L/{print "_"$0}' sample |
sort -r
_LAST ...
46m ...
02m22s ...
01m59s ...
01m15s ...
00m28s ...
00m07s ...
Others still appreciated.