You are not logged in.

#1 2015-11-27 13:26:35

briareos
Member
Registered: 2015-09-28
Posts: 9

Getting the last line output by a command

Oftentimes the last line output from a command is a file path I'd like to open, so I'd like to have some way of quickly accessing the contents of this line.

How can I get that line AFTER my command is finished (=> without using pipes), when I'm back to the prompt?


Note: Currently I have this function to **prefix** commands, but it's not bullet-proof and I have to know beforehand that I'll need the line.

   

t () { t=`$*|tail -n1`; echo "$t"; } ; alias v="vim '$t'"

=> Allows me to run things like

t find ./ ...

followed by

v

to open the file in vim, for example

Offline

#2 2015-11-27 15:57:38

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: Getting the last line output by a command

I don't think this is possible without patching bash to always store the last line of output.
Alternatively you could set up many aliases for the programs you use.

By the way, you should be enclosing variables correctly in brackets in your function, so that a single argument "a b" does not become two.
With some other changements, this is how I'd write such a function:

function t() { t="$("$@")"; test -n "$t" && echo "$t"; t="$(tail -1 <<< "$t")"; }

This will put all error messages on top, which you might want to cirvumvent by redirecting stderr to stdout.
To preserve the differentiation between stderr and stdout, one could also use `tee' writing to /tmp.

Offline

#3 2015-12-02 23:41:12

briareos
Member
Registered: 2015-09-28
Posts: 9

Re: Getting the last line output by a command

Thanks for your hints.

As I am always in a tmux session anyway, I now do it via a `xdotool` macro (assigned to `Alt+j`):

1. enter tmux copy-mode (`tmux copy-mode`)
2. go up one line and to line beginning
3. start visual selection
4. go to end of line - 1
5. copy & paste

This takes some 300 ms to execute, but that's better than typing it myself.

Offline

#4 2015-12-03 15:38:48

respiranto
Member
Registered: 2015-05-15
Posts: 479
Website

Re: Getting the last line output by a command

briareos wrote:

Thanks for your hints.

As I am always in a tmux session anyway, I now do it via a `xdotool` macro (assigned to `Alt+j`):

1. enter tmux copy-mode (`tmux copy-mode`)
2. go up one line and to line beginning
3. start visual selection
4. go to end of line - 1
5. copy & paste

This takes some 300 ms to execute, but that's better than typing it myself.

If it takes so long, are you sure the command is not reexicuted?

Offline

Board footer

Powered by FluxBB