3

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?

7
  • 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, 2013 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, 2013 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, 2013 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, 2013 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 http://unix.stackexchange.com/.
    – terdon
    Jan 8, 2013 at 19:33

1 Answer 1

0

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

Try grc.

http://korpus.juls.savba.sk/~garabik/software/grc.html

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.

https://unix.stackexchange.com/questions/8414/how-to-have-tail-f-show-colored-output?newsletter=1&nlcode=17737|321c

HTH

Found slightly related example code at https://unix.stackexchange.com/questions/26313/colored-find-output

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.

You must log in to answer this question.