<h1>edited</h1>
<p>I find myself frequently looking to see if something has stopped happening. To do this, it helps to see events in chronological order...</p>
<p><a href="https://stackoverflow.com/a/55858337/8656552">This solution</a> <em>seems</em> to work, but the formatting still drives me insane...</p>
<p>The solution I have &quot;sort of&quot; working -</p>
<pre><code>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
</code></pre>
<p>...this seems a bit like overkill to me.</p>
<p>The whitespace-delimited left-justified fields have no leading zeroes, report only seconds up to 2m as <code>[0-9]+s</code>, then report as <code>[0-9]+m[0-9]+s</code> up to 5m, after which it seems to report only <code>[0-9]+m</code>.</p>
<p>Anyone have a short, maybe even simple-ish, <em>easier to read</em> solution that works?<br />
No preference of tool (<code>sed</code>, <code>awk</code>, <code>perl</code>, native <code>bash</code>, etc), as long as it works and is likely to be already installed anywhere I need to work.</p>
<p>It's not a high priority, but seemed like a fun little challenge I thought I'd share.</p>
<p>My test data:</p>
<pre><code>$: cat sample
LAST ...
28s ...
2m22s ...
46m ...
7s ...
75s ...
119s ...
</code></pre>
<p>Result with desired output -</p>
<pre><code>$: 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 ...
</code></pre>
<p>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 &quot;eyeballing&quot; the data, so other formats are not a problem as long as it's easy to read.</p>
<p>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 <em>can</em> get the order from <a href="https://stackoverflow.com/a/55858337/8656552">this approach</a> I'm really only looking for elegant formatting options.</p>
<p>A clumsy adaptation of Daweo's <code>awk</code> solution with formatting -</p>
<pre><code>$: awk '/^[0-9]/{ if($1!~/m/){$1=&quot;0m&quot; $1}; split($1,arr,/m/);
        t=arr[1]*60+arr[2]; m=(t-(t%60))/60; s=t-(m*60);
        m=sprintf(&quot;%02dm&quot;,m); if(s){ s=sprintf(&quot;%02ds&quot;,s) } else s=&quot;&quot;;
        $1=sprintf(&quot;%s%s&quot;,m,s); print; } /^L/{print &quot;_&quot;$0}' sample |
   sort -r
_LAST ...
46m ...
02m22s ...
01m59s ...
01m15s ...
00m28s ...
00m07s ...
</code></pre>
<p>Others still appreciated.</p>

Paul Hodges
<p>I'd convert everything to seconds first and then print it as <code>HH:MM:SS</code>, e.g.:</p>
<pre><code>$ cat tst.awk
BEGIN {
    split(&quot;h m s&quot;,denoms)
    fmts[&quot;s&quot;] = fmts[&quot;m&quot;] = fmts[&quot;h&quot;] = &quot;%02d&quot;
    mults[&quot;s&quot;] = 1
    mults[&quot;m&quot;] = 60
    mults[&quot;h&quot;] = 60 * 60
}
sub(/^L/,&quot;_L&quot;) {
    print
    next
}
{
    time = $1

    secs = 0
    while ( match(time,/[0-9]+./) ) {
        value = substr(time,1,RLENGTH-1)
        denom = substr(time,RLENGTH)
        time  = substr(time,RLENGTH+1)
        secs += value * mults[denom]
    }

    for ( i=1; i in denoms; i++ ) {
        denom = denoms[i]
        out = (i&gt;1 ? out &quot;:&quot; : &quot;&quot;) sprintf(fmts[denom],int(secs/mults[denom]))
        secs %= mults[denom]
    }

    $1 = out

    print | &quot;sort -r&quot;
}
</code></pre>
<p>
<pre><code>$ awk -f tst.awk sample
_LAST ...
00:46:00 ...
00:02:22 ...
00:01:59 ...
00:01:15 ...
00:00:28 ...
00:00:07 ...
</code></pre>
<p>Obviously add the definitions for <code>&quot;d&quot;</code> in the <code>BEGIN</code> section if you want to include days and similarly for other longer durations.</p>
