6

I would like to take the output from a program and interactively filter which lines to pipe to the next command.

ls | interactive-filter | xargs rm

For example, I have a list of files that a pattern cannot match against to reduce. I would like a command interactive-filter that will page the output of the file list and I could interactively indicate which lines to forward to the next command. In this case each line would then be removed.

2 Answers 2

9
  1. iselect provides an up-down list, (as input from a prior pipe), in which the user can tag multiple entries, (as output to the next pipe):

    # show some available executables ending in '*sh*' to run through `whatis`
    find /bin /sbin /usr/bin -maxdepth 1 -type f -executable -name '*sh'   |
    iselect -t "select some executables to run 'whatis' on..." -a -m |
    xargs -d '\n' -r whatis 
    

    Output after pressing the spacebar to tag a few on my system:

    dash (1)             - command interpreter (shell)
    ssh (1)              - OpenSSH SSH client (remote login program)
    mosh (1)             - mobile shell with roaming and intelligent local echo
    yash (1)             - a POSIX-compliant command line shell
    
  2. vipe allows interactively editing (with one's favorite text editor) what goes through a pipe. Example:

    # take a list of executables with long names from `/bin`, edit that
    # list as needed with `mcedit`, and run `wc` on the output.
    find /bin -type f | grep '...............' | EDITOR=mcedit vipe | xargs wc
    

    Output (after deleting some lines while in mcedit):

       378   2505  67608 /bin/ntfs-3g.secaudit
       334   2250 105136 /bin/lowntfs-3g
       67    952  27152 /bin/nc.traditional
       126    877  47544 /bin/systemd-machine-id-setup
       905   6584 247440 total
    

Note on push & pull:

  • iselect starts with a list in which nothing is selected.
  • vipe starts with a list in which every item shown will be sent through the pipe, unless the user deletes it.

In Debian-based distros, both utils can be installed with apt-get install moreutils iselect.

0
4

You can write vipe in a few lines of shell. Quick-and-dirty proof-of-concept that works for me:

EDITOR=vi   # change to preferred editor as needed.

vipe()
{
  cat > .temp.$$
  if $EDITOR .temp.$$ < /dev/tty > /dev/tty 2>&1 ; then
    cat .temp.$$
  fi
  rm .temp.$$
}

Source this into your shell and there it is. The purpose of the if is to suppress generation of the output if the editor (or attempt to run the editor) fails.

You must log in to answer this question.

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