It's easier to achieve this with tools that are directly used together with the program that produces the output, instead of trying to scrape it back from what was already printed.
There are tools that can show an interactive "select item" or "select file" prompt and are designed to feed it back into the shell as a command. fzf
and broot
are two examples.
fzf
can accept any input through stdin and let you interactively filter through it, for example:
cd "$(find -type d | fzf)"
This can be wrapped in a shell function, or used as an enhanced Tab-complete (Bash code for this is included with fzf), or even bound to a custom keyboard shortcut, e.g. Alt+D to select a directory via fzf and insert it at cursor position (my aliases.sh has an example for Bash, though it would need to be done very differently for Zsh).
broot
on the other hand is specifically a file tree browser, but is also designed to be used through a shell function wrapper.
broot --print-shell-function >> ~/.bashrc
Running the newly created br
function will open a file browser, in which you selecting a folder with Alt-Enter will cause the parent shell to cd
to the selected location.
sed -n 1p | pbcopy
if I want the first line, or changing that1
to a3
if I want the third line, and so on.