2

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.

  • 7
    Have you looked into top -b? – DopeGhoti Dec 5 '17 at 22:49
  • Parsing top output is not really a good idea for monitoring. – Patrick Mevzek Dec 6 '17 at 2:10
  • @PatrickMevzek please provide better alternative. – Putnik Dec 6 '17 at 21:31
  • It depends on what numbers do you want (it do not know what "performance data" is) and what you do with them. top is only a frontend using values in collected from various files in /proc/ and /sys/. For example see /proc/meminfo, /proc/loadavg or /proc/stat. There are libraries and tools using and parsing that. – Patrick Mevzek Dec 6 '17 at 21:34
  • @PatrickMevzek I thought it is obvious from second screenshot. I need idle, IOwait and stolen CPU %. What tool can I use? – Putnik Dec 6 '17 at 21:40
3

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:

  • 'Setting the TOPCOLORS' is the exact answer because 'z' turns it even more colorful. Thanks! – Putnik Dec 6 '17 at 21:41
0

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.