Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a tool that will let me pipe the output of an executable through it and play a sound or, better yet, highlight the line where a selected phrase occurs?

I want to be able to do something like this:

./myProgWithLotsOfOutput  | ./myMysteryTool blahUsefulWord

and have all of the output appear and scroll by, but any time "blahUsefulWord" appears, the console beeps and highlights that line.

It's kind of a mix of tee and grep. Alternatively, do you have the BASH skills to whip something like this up?

share|improve this question

For Bash >= version 3.2:

regex='some regex pattern'
highlight=$(tput setaf 2) # green, use setab to do inverse instead of foreground
off=$(tput sgr0)
while read line
    [[ $line =~ $regex ]] && echo -n $'\07'"$highlight"
    echo "$line$off"

It highlights the whole line rather than just the match.

share|improve this answer

Based off of this Q&A, I've created the following function:

# mylite since 'highlight' is already a program
function mylite () {
    pattern=$1; shift; file=$1
    grep -E --color "$pattern|$" $file

Usage examples:

mylite pattern file.txt
cat file.txt | mylite pattern

No beeping in this example, but I'm sure that can be added by modifying grep as shown by other answers on this page.

share|improve this answer

My mistake on the answer. I tried this:

export GREP_COLOR='1;37;41' # set to a sequence of xterm escape sequences for colorization.
export GREP_OPTIONS=--color=always

while read LINE
    echo $LINE | grep $PATTERN || echo $LINE
share|improve this answer
But grep will only show the lines that match, right? I want to see all of the output with the matching lines highlighted. – Aug 13 '10 at 15:47
To ring the bell on each match: echo $LINE | grep $PATTERN && echo -n $'\07' || echo $LINE. However spawning grep for each line could be expensive. – Dennis Williamson Aug 13 '10 at 16:48
To see all the output with the matching lines highlighted, use grep -C999 $PATTERN. That assumes that there are no more than 2 * 999 lines between matches. If you want the whole line containing the match highlighted, use grep -C999 ".*$PATTERN.*". – garyjohn Aug 14 '10 at 7:35
Rather than spawning a grep for each line, you could use sed to insert a bell on each colored line, like this: grep -C999 --color=always $PATTERN | sed '/^[/s/^/^G/'. The ^[ is a literal Esc and ^G is a literal BEL, or Ctrl-G. Those characters can be entered into the sed command by typing Ctrl-V before each one. – garyjohn Aug 14 '10 at 7:50

Something like logwatch may be what you are looking for.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .