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

How can I filter the output from a "self-updating" program such as top while keeping its functionality intact?

For example, I want to highlight my user name in top's output. My idea is to use something such as this:

top | grep --color -E "user|$"

It works insofar as the user name gets indeed highlighted, however, the cursor isn't where it should be in the top program and the last line is missing.

I have had similar results with other interactive shell programs such as wget (trying to highlight the progress bar using escape sequences) and rsync. How can I properly filter output from an interactive program?

share|improve this question
My guess is that you can't, but lets see what SU has to say. You can work around this using top's batch mode (-b). – terdon Jan 8 '13 at 19:13
I'm aware of batch mode, but it is not what I am looking for. I really hope you are wrong and it is possible after all. If it isn't, it's a serious limitation in bash/top/UNIX(?). – user185429 Jan 8 '13 at 19:17
Well no, that's not fair. Interactive programs are not designed to have their output piped but to be read interactively. I don't kn9ow the details but they must open some temporary output buffer and update its contents. Hardly fair to blame that on *nix, piping is for data written directly to STDOUT. – terdon Jan 8 '13 at 19:27
"Interactive" programs use STDOUT just like all others. They employ ANSI escape sequences to update their screen output in the terminal so they appear interactive but they still write to STDOUT. If they didn't, the above command wouldn't work at all, but it does – just not perfectly. – user185429 Jan 8 '13 at 19:30
Oh, OK, thanks for the info. In that case it may well be possible somehow. If you don't get an answer here after a while, try your luck at – terdon Jan 8 '13 at 19:33
up vote 0 down vote accepted

No idea if this will work (I've never used it).

Try grc.

I saw it in this post where they got it to work with tail -f. It also mentions a number of other possible things to try.|321c


Found slightly related example code at

replaced the find with top and fooled around with the case statement a little bit like having *bigbird*) as a case (my user name). The script was not happy with me, but it did produce some colorized output (along with error messages because I was just hacking it up and not really coding it properly). It does appear to be (at least some) proof of concept.

share|improve this answer

You must log in to answer this question.