0

Is there a tool similar to choose that I can plug into chain of commands that will let me select (multiple) lines for the next step?

choose unfortunately doesn't "pass on" the line you select, instead if you do a command, select a line and try to do something with it, what you get is:

% echo "a\tb\ncde\tf" | choose | wc
cde      f
       0       0       0
1
  • There's also a completely shell based solution to this problem (currently only single line select as well): Sentaku Apr 30, 2014 at 16:11

3 Answers 3

1

It's open source, and the basic logic isn't very complicated, so you could always just modify it.

Put the following lines into a file (say, choose.diff) in the same directory as choose, then run patch -p1 < choose.diff:

--- a/choose
+++ b/choose
@@ -164,8 +164,8 @@ def do_it(auswahl):
     index = select_entry(auswahl,
                          header_text=u'Navigate by pressing ↑ and ↓, select by pressing Enter')

-    # print chosen string
-    print(orig_auswahl[index])
+    # return chosen string
+    return orig_auswahl[index]


 if __name__=="__main__":
@@ -179,7 +179,7 @@ if __name__=="__main__":
     sys.__stdin__ = sys.stdin = open('/dev/tty')
     os.dup2(sys.stdin.fileno(), 0)

-    do_it(auswahl)
+    choice = do_it(auswahl)

     #restore old stdout
     sys.stdout.flush()
@@ -190,3 +190,5 @@ if __name__=="__main__":
     sys.__stdout__ = sys.stdout = old_out
     sys.__stdin__ = sys.stdin = old_in
     sys.__stderr__ = sys.stderr = old_err
+
+    print choice
1
  • Thanks! And by combining this with the browse.py example code in the urwid repository, I was also able to get multi-select. Apr 17, 2014 at 22:39
0

Not sure this suits you, but if it's about selecting lines from a stream of text, sed should always sort you out. Check the examples below:

I want to output only those lines not matching a certain regular expression (regex) - in this example, omit all lines containing the word "pipe".

$> echo -e "this is a line\nthat is a line\nthis is a piped line\nthat is a line in a pipe" | sed '/pipe/d'
this is a line
that is a line

In the next one, output only those lines containing the word "pipe"

$> echo -e "this is a line\nthat is a line\nthis is a piped line\nthat is a line in a pipe" | sed '/pipe/!d'
this is a piped line
that is a line in a pipe

Alternatively, ouput only the 2nd and 3rd lines:

$> echo -e "this is a line\nthat is a line\nthis is a piped line\nthat is a line in a pipe" | sed -n '2,3p'
that is a line
this is a piped line

And many more examples and possibilites... Check out sed1line

Definitely, there are many more capable alternatives too, awk, perl, etc

0

You're looking for fzf!

It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Note that you can filter interactively by writing expressions, in addition to arrow navigation. You'll want to call fzf with -m/--multi for multi-selection.


Also, see related projects for tools people have built on top of fzf, linked from the bottom of the readme.

It is available in debian & ubuntu default packages now, but before Debian 10 Buster and Ubuntu 19 or 20, pick was the closest analogue available.

I highly recommend setting up fzf as a default history selector, ctrl-r, per keybindings instructions on readme, which are default set up by manual install script, and not by debian/ubuntu packages, however this can be done manually, readily for bash/zsh/fish/vim.

Bash:

Append this line to ~/.bashrc to enable fzf keybindings for Bash: source /usr/share/doc/fzf/examples/key-bindings.bash

Append this line to ~/.bashrc to enable fuzzy auto-completion for Bash: source /usr/share/doc/fzf/examples/completion.bash

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .