4

I'm trying to process 'top' output to set CPU performance data. When I just grep the output it is colorized: enter image description here or enter image description here The numbers are bold, and it adds a lot of unnecessary data to the output: enter image description here

I tried to strip the color codes answer but it does not work. I want to avoid other much more complex answers in that question for sake of performance.

I've tried to disable colors by switching term mode but no luck: enter image description here

So how can I disable the color output?

PS: I found how to get data: I can awk only numbers then it works, but still wonder if there is any way to disable color here.

Kusalananda
  • 333,661
Putnik
  • 886

2 Answers2

5

Here's one way to disable colored output in top:

Step 1: Run top

Step 2: Press the z key to toggle the color mode

Step 3: Press the W key to save the new setting

For reference, look at the top man page, specifically Section 4: Interactive Commands. There you will find the following descriptions of these two interactive commands:

W  :Write-the-Configuration-File
    This will save all of your options and toggles plus the
    current display mode and delay time.  By issuing this command
    just before quitting top, you will be able restart later in
    exactly that same state.

z  :Color/Monochrome toggle
    Switches the `current' window between your last used color
    scheme and the older form of black-on-white or white-on-black.
    This command will alter both the summary area and task area
    but does not affect the state of the `x', `y' or `b' toggles.

Also see these related posts:

igal
  • 9,886
  • 'Setting the TOPCOLORS' is the exact answer because 'z' turns it even more colorful. Thanks! – Putnik Dec 06 '17 at 21:41
4

The command you are looking for is:

top -bn1

-b is for batch mode - i.e. designed to be parseable without ascii escape colour sequences.

-n is the number of counts -n1 represents a one time process dump.

Unrelated: to suppress colours in ls, use \ls. For any other commands, \grep will also suppress the output colours. It is possible for you to pipe command output through like this: ls | \grep . and this will remove any coloured output from a command.

Owl
  • 215