I want to search and cut text from command output in a terminal so I can use it in subsequent command line invocations or just in an editor session.

Is there any way to easily run a regular expression against the output of a terminal and cutting results without touching the mouse?

My requirements are:

  • it should work while working with remote machines through ssh. In those machines I can not install extra software there so the solution must be local.
  • if possible, it must be "always present". For instance, if I'm running a long process and suddenly it fails I'd like to be able to grep through the output without having to start the process again with a prepared environment.
  • 1
    Please remember to accept/upvote the best answer to your question (tick/check mark on the left). This way, the question is marked as "answered" and future readers can refer to it knowing the solution works. Thank you...:) – ish Jul 8 '12 at 3:35

screen(1) allows you to copy/paste using the keyboard, but it requires that you run your shell/process inside screen.

To copy, hit Ctrl+A [, move the cursor (using the arrow keys) to the start point and hit (RETURN), move the cursor to the end point and hit . The marked text is now copied.

To paste, hit Ctrl+A ]

This solution also applies to Byobu, since Byobu is just a wrapper around screen.


xclip and grep and pipes!

  • Install the xclip utility (sudo apt-get install xclip)

    • By default, xclip is set to copy piped input to the clipboard
    • xclip -o will "paste" the contents of the clipboard into your terminal (i.e. standard output)
  • For regular expressions, just pipe the command to grep; the -P switch will give you full Perl regex functionality.

  • Example: ls -lR | grep -P "anc.*" | xclip to copy to clipboard

    • Use tee xclip instead so that it gets displayed AND copied.
  • I didn't know about xclip and it seems useful but it doesn't solve my current problem. See the updated question. – tonicebrian Jun 27 '12 at 7:33
  • @ancechu: Well, that's why you should explain as much of the context as possible in the question to start with. Do you ssh from Windows, if so which program? Or just from gnome-terminal, etc.? – ish Jun 27 '12 at 9:55
  • I'm working from a Linux machine, mainly using terminator and ssh-ing to remote boxes – tonicebrian Jun 27 '12 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.