4

Possible Duplicate:
Removing control chars (including console codes / colours) from script output

If we redirect output to file in this pattern top >> somefile.txt, we get lot of garbage value in the output. But for top there is batch (top -b -n1 >> somefile.txt) option where the file output is as desired. In case of programs like nethogs there is no such options. Is there is good way to output in such case.

  • 1
    I would say that the best thing to do is to write to the authors of nethogs requesting they add a mode similar to top -b. – Random832 Oct 08 '12 at 15:09
  • I created a comment saying this is not a duplicate, but when I try to edit the title of this question to express the real question it becomes "How to clean the output of an ncurses program?", which is clearly a duplicate. Voted to close. – phunehehe Oct 09 '12 at 12:59
  • for nethogs, may be these options are new? nethogs -t or nethogs -b, check here also – Aquarius Power Jun 18 '14 at 23:40

2 Answers2

4

You can also try filtering out the garbage yourself. For example, this would remove all the non-printable characters:

nethogs | sed 's,[^[:print:]],,g' >> somefile.txt

You can extend that as needed or take the opposite approach and only allow explicit non-garbage to come through, eg. (alphanumerics and punctuation):

nethogs | sed 's,[^[:alnum:][:punct:]],,g' >> somefile.txt

If the output is color coded, some extra work is needed to strip out the color escape strings.

lynxlynxlynx
  • 3,373
2

There isn't a good way, but sometimes setting the terminal can help.

E.g.,

TERM=dumb top

gets rid of most control characters.

Julian
  • 909
  • 5
  • 5