5

How can we limit the number of rows in the top command output to a specific number 'say 5' in non interactive mode ?

Mathews Jose
  • 399
  • 6
  • 18

3 Answers3

7

You must provide the -w option without an argument and specify the desired number of lines through the LINES environment variable:

LINES=20 top -b -n 1 -w

Note that the width of the output is controlled through the COLUMNS environment variable. So if it is set in your environment, you must override it for the invocation of top:

LINES=20 COLUMNS=80 top -b -n 1 -w

or

(unset COLUMNS; LINES=20 top -b -n 1 -w)

This works for the version of top included in Ubuntu 15.10.

$ top -v
  procps-ng version 3.3.9
Usage:
  top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]

Evidently the -w option is not present in all versions of top. But you can always use pipes to limit the output:

top -b -n 1|head -n 20
Leon
  • 31,443
  • 4
  • 72
  • 97
  • Produces `t T % K K` (each on a separate line) for me on Linux. – Kusalananda Jul 29 '16 at 08:46
  • @Kusalananda Do you have the environment variable **COLUMNS** set to 1? – Leon Jul 29 '16 at 08:47
  • `env COLUMNS=80 LINES=20 top -b -n 1 -w` produces the same output as before. `env LINES=20 top -b -n 1 -w 80` gives me more than 20 lines (all processes). – Kusalananda Jul 29 '16 at 08:49
  • I would also suggest to make a back up of `LINES` as other processes make use of it – sjsam Jul 29 '16 at 08:49
  • 1
    @sjsam Setting it like that, before a command, will set it for the executed process, not in the calling shell. – Kusalananda Jul 29 '16 at 08:50
  • I am getting below error : ` LINES=20 COLUMNS=80 top -b -n 1 -w top: unknown argument 'w' usage: top -hv | -abcHimMsS -d delay -n iterations [-u user | -U user] -p pid [,pid ...] ` any idea – Mathews Jose Jul 29 '16 at 08:54
  • @Leon : Also note that this is not the substitute for the `n` option while within top. Perhaps the op is looking for batch mode as I understand from their comment below the question. ++ :) – sjsam Jul 29 '16 at 08:55
  • Actually I made a correction in Question ..mine is a Linux machine ..does it matter ? I am getting error as -w option is not available – Mathews Jose Jul 29 '16 at 08:56
  • @MathewsJose What is your version of top? – Leon Jul 29 '16 at 08:57
  • @Leon I may pipe the results to `sed` to strip the content of the header like `LINES 27 top -b -n 1 -w | sed -n '1,7!p'` . The top header spans 7 lines in my top `version 3.3.9` ` – sjsam Jul 29 '16 at 09:04
  • @Leon version is 3.2.8 – Mathews Jose Jul 29 '16 at 09:05
  • @MathewsJose It is older than mine and probably doesn't support the -w option. You can limit the output with a pipe: `top -b -n 1|head -n 20` – Leon Jul 29 '16 at 09:07
  • @Leon yea Leon ..I think so ..I cant rely on -w option :( as it is not available in my installation ..I think now have to go with piping the output – Mathews Jose Jul 29 '16 at 09:08
2

For an OS X answer:

$ top -l 1 -n 5

But the OS X top sorts on PID by default, so you probably want to add -o cpu too.

It seldom gets the %cpu right on the first iteration either... which I think might be an issue with top on other systems too.

Depending on what the actual data is that you'd like to get at, there might be better ways of getting it. Parsing the output from an interactive program is suboptimal.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
2

I use a trick, specially for batch mode. I pipeline the exit to grep, with option "-A", to show N lines after match.

As in the first line of top there is something like : "load average", I grep that, for instance :

$ top -d 5 -b|grep "load average" -A 15
top - 09:42:34 up 38 min,  1 user,  load average: 0.22, 0.39, 0.53
Tasks: 294 total,   2 running, 291 sleeping,   0 stopped,   1 zombie
%Cpu(s):  3.5 us,  0.9 sy,  0.0 ni, 94.6 id,  0.5 wa,  0.3 hi,  0.1 si,  0.0 st
KiB Mem :  8065144 total,  2213800 free,  2733524 used,  3117820 buff/cache
KiB Swap: 24575996 total, 24575996 free,        0 used.  4613128 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 2744 lrojas    20   0 3376820 752000 116588 R  20.2  9.3   9:30.01 firefox
 1869 lrojas     9 -11  566164  18336  14300 S   5.2  0.2   2:35.78 pulseaudio
 2401 lrojas    20   0  740092 200456  87256 S   2.4  2.5   0:57.29 skype
 2402 lrojas    20   0  617872 172924  76172 S   2.2  2.1   0:57.17 skype
 1333 root      20   0  459028  60992  48024 S   1.6  0.8   0:36.14 Xorg
 1838 lrojas    20   0 2103336 184468  64724 S   1.4  2.3   0:56.85 gnome-shell
 2359 lrojas    20   0  741212  35068  24620 S   1.4  0.4   0:06.83 gnome-terminal-
 2404 lrojas    20   0 1867556 229912  83988 S   0.8  2.9   0:19.63 thunderbird
 1249 apache    20   0  461436  10196   3404 S   0.4  0.1   0:00.57 httpd

This way it will continue in batch mode, always showing only the first N lines of output.

Completely standard solution, for any version of top.

Cheers,

Luis

Kalki70
  • 467
  • 4
  • 11