views:

2158

answers:

4

I want to use part of the output of a command run from the command line in another xterm, or as part of a different command. For instance:

> grep error error.log
error: can't find file ~/<some very long path>/thisfile

and I want to do this:

>ls ~/<some very long path>/

I know two ways to do this:
1. copy ~/<some very long path>/ with the mouse.
2. use some combination of head/tail/awk/sed/perl/cut/etc... to extract only what I need from the output and then use that inside backticks.

Is there any way to copy text without using the mouse? The example that comes to mind is visual mode inside VIM, but I don't know how to do that inside the xterm.

+11  A: 

You can use GNU screen's copy and paste commands.

Quick tutorial:

  1. Open screen: screen (or screen myprog my args here)
  2. Run your program, producing output you want copied
  3. Enter copy mode: ^A [
  4. Move your cursor to the start point
  5. Hit enter
  6. Move your cursor to the end point
  7. Hit enter
  8. Paste: ^A ]

Screen is much more powerful than that (I use it to tab several virtual terminals without the need for a special terminal emulator, and also so that I don't loose my sessions when X crashes or something). To get out of screen, simply end your shell session, or type ^A ^.

strager
+3  A: 

The OP said he didn't want to do this, but here is a unix utils way to do it for posterity. If you learn these little unix utilities they can be quite powerful.

ls $( dirname $( grep error error.log | head -1 | cut -d " " -f 5- ) )

note: syntax from memory

Quog
+5  A: 

I find XSel is useful in similar situations. It's a tool that manipulates the X selection. For example, this will store the output of your command to the clipboard:

grep error error.log | xsel -bi
salty-horse
+1  A: 

You may send output directly to the concerned xterm using xterm device no. Here is the command.

xyz$ls /home/ankit/documents/etc/x/y/z > /dev/pts/0

(or watever is the device name, You can get it using command 'w' for opened xterm)

Try it & hope this may solve your problem.

Ankit S
This doesn't make the output available for re-use.
Dennis Williamson