Repeat output of last command w/o repeating last command

Is there a way to repeat the output of the last command for filtering without running the command again? All I could think of was to copy all the data to a text file and process it that way, is there another way? Like say I want to grep server.server.lan from a dtrace that was pages long after I already ran it, is there a way to filter output after its already been sent to terminal?

You can consider to use the tee command.

Another way - capture the result in a variable

result=$(mycommand)
echo "$result" | grep "something"

This won't help your current problem. Just meant as a way to solve it in the furutre.

As said before: 'tee' is your friend (example in tcsh)

  > alias cache '\!* | tee .cache'
  > alias repeat 'cat .cache'
  > cache ls
  > repeat ls 

I am sure bash allows something similar. Automatic cleaning of the .cache is left as an excercise to the reader :wink:

Not quite, bash and ksh aliases won't accept parameters. But that's what functions are for:

function cache {
    exec 3>&1
    ( "$@" | tee ."$( basename $1 )".out >&3 ) 2>&1 | \
        tee ."$( basename $1 )".err >&2 3>&1
    3>&-
}

This'll redirect regular output to one file (eg. .ls.out if you cache ls), and stderr to another (eg. .ls.err), while leaving the regular output unaffected (stdout and stderr can be filtered just the same as always).

Redirection to the third channel blatantly stolen from Csh Programming Considered Harmful, as I can never remember the exact syntax.

Hi Pludi :slight_smile:

Ah, sure - functions in bash are great. And I hate that tcsh does not allow to redirect out/err individually :frowning:

Cheers, Andre