xsv icon indicating copy to clipboard operation
xsv copied to clipboard

Feature suggestion: add support for first and last N records to slice

Open joehorsnell opened this issue 5 years ago • 4 comments

Hi @BurntSushi,

Thanks for your fantastic efforts on xsv (and ripgrep and others!).

As far as I'm aware, there isn't an easy way to get the last N records (i.e. logically tail of the dataset) using slice? You can obviously get the first N records using --start and --len, but would you see value in adding support for --first and --last to slice, to more easily get a contiguous block of rows. If so, would you accept a PR to add this?

Cheers,

Joe.

joehorsnell avatar Jan 31 '20 10:01 joehorsnell

Maybe --start can just accept negative numbers?

BurntSushi avatar Jan 31 '20 11:01 BurntSushi

Maybe --start can just accept negative numbers?

Yes indeed, as one can index arrays using negative values in Ruby and, I’m sure, other languages. That’s a good suggestion 👍 and probably simpler, if arguably slightly less intuitive, unless you know the convention. Obviously the help text would show this.

I’ll try and take a look, cheers.

joehorsnell avatar Feb 01 '20 11:02 joehorsnell

@joehorsnell would it be clever to add full slicing support (as in python/ruby/...): .. --slice 15:22 # gives rows 15 to 22, if len>22 else len (...) .. --slice :-13 # gives everything except the last 13 rows .. --slice -13: # returns last 13 rows .. --slice 1:10:3 # returns every third row between rows 1 to 10 .. --slice 1:10:-1 # returns every row between rows 10 to 1 (reverse, because negative stride!)

cons0l3 avatar Feb 01 '20 13:02 cons0l3

Good suggestion @cons0l3 👍. I guess that would more be --range maybe, since the subcommand is already called slice?

I'll try and take a look first at doing the simpler negative index idea, then see how complicated your more advanced suggestion is.

joehorsnell avatar Feb 04 '20 23:02 joehorsnell