xsv
xsv copied to clipboard
Feature suggestion: add support for first and last N records to slice
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.
Maybe --start
can just accept negative numbers?
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 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!)
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.