I encountered the 'color-stealing' issues with watch, it's because watch uses a simple /bin/sh output style, which clobbers any color, aliases, shortcuts... the whole shebang!
So I cobbled this together, placed in my .bashrc, allows me to use all my own aliases, shortcuts and whatnot:
Usage:
watcher 20 'somecommand | apipe | grep'
The number is the delay between updates, in seconds, the command can include anything you can quote.
###### 'watch' workalike, lets me use my aliases and such
function watcher() { WATCHERTIME=$1 ; WATCHERFILE=/tmp/watcher$$ ; shift ; while true ; do WATCHERHEIGHT=$(($LINES - 5)) ; ( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null; clear ; /bin/echo -n "Every ${WATCHERTIME} seconds - " ; date ; /bin/echo ; cat ${WATCHERFILE} ; \rm -f ${WATCHERFILE} ; /bin/echo ; /bin/echo "==" ; sleep ${WATCHERTIME} ; done ; }
To break it down:
function watcher()
{
WATCHERTIME=$1
WATCHERFILE=/tmp/watcher$$
shift
while true; do
WATCHERHEIGHT=$(($LINES - 5))
( eval $* ) | tail -n ${WATCHERHEIGHT} > ${WATCHERFILE} 2>/dev/null
clear
/bin/echo -n "Every ${WATCHERTIME} seconds - "
date
/bin/echo
cat ${WATCHERFILE}
\rm -f ${WATCHERFILE}
/bin/echo
/bin/echo "=="
sleep ${WATCHERTIME}
done
}
It determines the current screen height in lines, subtracts out enough for its own output, then repeatedly executes the command given, then clears the screen, displays the output and waits for the next loop. It displays a short '==' at the bottom to signify that's where the output ended. Sometime's it's helpful to know this.
I did it this way so there was smallest possible delay when displaying. if you don't capture output and then display, you get a long pause, then the output... icky.
Since it doesn't clobber colors, you get everything you're used to getting. Enjoy!
Don't use shell loops to process text, that's bad practice.
The shell's job is to run commands (the right ones) and make them cooperate to a task.
Here, the right command is the practical extraction and report language interpreter called once, not several commands run (in sequence!) for each line of a file.
perl -lne '
if (/(.*?) \[.*getConnection (.*?), (ID=\d+)/) {
if ($2 eq "REQUEST") {$r{$3}=$1}
elsif ($2 eq "DONE") {print "$3, getConnection $r{$3} $1" if $r{$3}}
}' < your-file
Best Answer
Your command:
Your problem:
You're inserting a newline before
$time
. Stop that. Do: