rcoh / angle-grinder

Slice and dice logs on the command line
MIT License
3.42k stars 72 forks source link

sort could cater for version numbers #134

Open daniejstriata opened 3 years ago

daniejstriata commented 3 years ago

With "sort -V" sort will properly sort anything with a version (as used in one of the examples). I seems that ag does not use the same std output as when I use bash Output Grouping I get no output:

curl  https://api.github.com/repos/rcoh/angle-grinder/releases  |    jq '.[] | .assets | .[]' -c |    agrind '* | json
         | parse "download/*/" from browser_download_url as version
         | sum(download_count) by version | sort by version desc' | **{ head -n1; sort -n; }**

version _sum

The example mentions a sort with this output but could it not include version sorting as provided by sort -V?

Is sorting already built in and I should not be using bash's sort to sort versions?

Regular sort: v0.10.0 66 v0.11.0 29 v0.12.0 172 v0.13.0 118 v0.14.1 15 v0.15.0 127 v0.2.0 25 v0.2.1 23 v0.3.0 30 v0.3.1 32 v0.3.2 26 v0.3.3 24 v0.4.0 22 v0.5.0 26 v0.5.1 21

Sort -V v0.2.0 25 v0.2.1 23 v0.3.0 30 v0.3.1 32 v0.3.2 26 v0.3.3 24 v0.4.0 22 v0.5.0 26 v0.5.1 21 v0.6.0 25

rcoh commented 3 years ago

that's a good point—generalized human sorting would be also be a nice enhancement. Not sure when I'd get to this personally but I'd be happy to accept a PR.

I'm not sure what's going on with your bash expression. Here's an alternative that worked for me:

 # skip the header
| tail -n +3 | sort -V

My guess is maybe it's confusing the TTY detection machinery in your case?

rcoh commented 3 years ago

probably the cleanest way to add this would be adding an optional human argument to sort that would trigger version/human and other sorts of smart orderings

daniejstriata commented 3 years ago

Note on the Output Grouping example I posted, I've never noticed but when you run it with cat it does not work. I've always used Output Grouping with ps to sort the output but still retain the header ontop in the right place.

ps -faux | { head -n1; sort -k2; } | head -n6

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 168340 12268 ?        Ss   15:50   0:02 /sbin/init splash
root        1018  0.0  0.0      0     0 ?        I<   15:51   0:00  \_ [cfg80211]
root        1023  0.0  0.0      0     0 ?        S    15:51   0:00  \_ [irq/168-iwlwifi]
root        1024  0.0  0.0      0     0 ?        S    15:51   0:00  \_ [irq/169-iwlwifi]
root        1025  0.0  0.0      0     0 ?        S    15:51   0:00  \_ [irq/170-iwlwifi]

I thought I would be able to sort ar output the same way but looking at another example I see it does not work as I expected. I'll play around with the output grouping. This is not related to ar. cat untitledt | { head -n1; sort -r; } Regular sort: It prints the single entry without the rest of the versions sorted.