hck icon indicating copy to clipboard operation
hck copied to clipboard

[feature request] select rightmost/trailing indices

Open jskrzypek opened this issue 3 years ago • 2 comments

This might not be feasible, but I'd really like to be able to select rightmost/trailing fields from each line. My use-case is for flattening an arbitrarily nested tree of files, where I only care about the parent directory and the filename of the files.

EX: Imagine this is the file structure

a
|-b
| |-c.jpg
| \-d
|   |-e.jpg
|   \-f.jpg
|-g
| |-h
| | \-i.jpg
| \-j.jpg
\-k.jpg

If I ran find ./a -type f -regex '.*\.jpg' on in the parent of a/ it would produce this output:

./a/b/c.jpg
./a/b/d/e.jpg
./a/b/d/f.jpg
./a/g/h/i.jpg
./a/g/j.jpg
./a/k.jpg

I want to generate the following output by selecting the 2 rightmost fiels from this input stream when splitting on / and joining on _ (i.e. quivalent to hck -d/ -D_ ...)

b_c.jpg
d_e.jpg
d_f.jpg
h_i.jpg
g_j.jpg
a_k.jpg

Would this be feasible? I can think of a couple signifiers for the potential option flag if it is, but my preference would be +f 2,1 (i.e. an inverted -f).

jskrzypek avatar Jun 17 '22 20:06 jskrzypek

Thanks for making an issue!

I think this should be doable - I don't have bandwidth to do this right this second, but will try to work it in at a future point.

(writing this out for future self of anyone inspired enough to do a PR) Within hck there are a few different code-paths to parsing lines based on the parameters chosen, with different levels of optimization applied to each. An inverted -f will need its own set of codepaths, one for the regex delimiter and one for the substr literal delimiter that reverses the input string before splitting (or figure out how to .rev the split iterator. See https://github.com/sstadick/hck/blob/master/src/lib/line_parser.rs

It may be tricky to specify output order... but also doable.

sstadick avatar Jun 20 '22 14:06 sstadick

Doable is what I like to hear!!

jskrzypek avatar Jun 28 '22 17:06 jskrzypek