# Filtering top output



## Graaf_van_Vlaanderen (Dec 21, 2009)

When using top, you normally get something like this:


```
last pid: 51918;  load averages:  0.00,  0.00,  0.00                                       up 0+02:39:09  17:32:37
35 processes:  1 running, 34 sleeping
CPU 0:  0.0% user,  0.0% nice,  0.4% system,  0.0% interrupt, 99.6% idle
CPU 1:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
Mem: 30M Active, 206M Inact, 152M Wired, 360K Cache, 111M Buf, 590M Free
Swap: 2009M Total, 2009M Free

  PID    UID    THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
 1013   1001      1  44    0  8304K  2268K CPU0    0   0:10  0.00% top
  819      0      1  44    0  5864K  1272K select  0   0:08  0.00% powerd
  986   1001      1  44    0 37040K  5168K select  1   0:02  0.00% sshd
51851   1001      1  76    0   139M 28988K ttyin   1   0:01  0.00% octave-3.2.3
  994   1001      1  44    0 37040K  5172K select  0   0:00  0.00% sshd
 1002   1001      1  44    0 37040K  5172K select  1   0:00  0.00% sshd
  995   1001      1  44    0 10288K  2792K pause   0   0:00  0.00% csh
51876      0      1  48    0 37040K  5132K sbwait  0   0:00  0.00% sshd
  999      0      1  49    0 37040K  5108K sbwait  0   0:00  0.00% sshd
  991      0      1  48    0 37040K  5108K sbwait  0   0:00  0.00% sshd
 1003   1001      1  44    0 10288K  2824K ttyin   0   0:00  0.00% csh
  983      0      1  47    0 37040K  5080K sbwait  1   0:00  0.00% sshd
  860      0      1  44    0  6920K  1600K nanslp  0   0:00  0.00% cron
  618      0      1  44    0  5992K  1528K select  0   0:00  0.00% syslogd
  987   1001      1  44    0 10288K  2704K pause   0   0:00  0.00% csh
51880   1001      1  44    0 10288K  2760K pause   0   0:00  0.00% csh
51879   1001      1  44    0 37040K  5196K select  1   0:00  0.00% sshd
51914   1001      1  44    0  6100K  1672K ttyin   0   0:00  0.00% more
51912   1001      1  47    0  7232K  1728K wait    0   0:00  0.00% sh
  851      0      1  44    0 25108K  4156K select  0   0:00  0.00% sshd
51911   1001      1  45    0  5884K  1344K wait    1   0:00  0.00% man
51913   1001      1  45    0  6968K  1180K pipdwt  1   0:00  0.00% zcat
  499      0      1  44    0  2180K   652K select  1   0:00  0.00% devd
  935      0      1  76    0  4776K  1504K select  1   0:00  0.00% dhclient
  907      0      1  76    0  5860K  1276K ttyin   1   0:00  0.00% getty
  909      0      1  76    0  5860K  1276K ttyin   1   0:00  0.00% getty
  913      0      1  76    0  5860K  1276K ttyin   0   0:00  0.00% getty
  908      0      1  76    0  5860K  1276K ttyin   0   0:00  0.00% getty
  912      0      1  76    0  5860K  1276K ttyin   0   0:00  0.00% getty
  914      0      1  76    0  5860K  1276K ttyin   0   0:00  0.00% getty
  910      0      1  76    0  5860K  1276K ttyin   1   0:00  0.00% getty
  911      0      1  76    0  5860K  1276K ttyin   1   0:00  0.00% getty
  147      0      1  76    0  2736K  1060K pause   1   0:00  0.00% adjkerntz
  964     65      1  76    0  4776K  1648K select  1   0:00  0.00% dhclient
  474      0      1  76    0  7012K  1368K select  1   0:00  0.00% moused
```

How can make only visible the percentage CPU output of one single process?

Currently I'm using this to output only one process:


```
top -U depaepe -b -d 2 | grep 51851
```

Then I get this:


```
51851 depaepe       1  76    0   139M 28988K ttyin   1   0:01  0.00% octave-3.2.3
51851 depaepe       1  76    0   139M 28988K ttyin   1   0:01  0.00% octave-3.2.3
```

In the end I would like to see only '0.00%' for each line, so when I redirect this to a file, I end up with one column, which makes it easy to load in GNU Octave. I tried also with 'sed', but still with little success. Any suggestions?


----------



## SirDice (Dec 21, 2009)

Why use top for this?

[cmd=]ps -u <pid>[/cmd]


----------



## Alt (Dec 21, 2009)

You can use ps:

```
# ps axu
USER    PID %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND
www   17732  4,2  0,1 32296 20540  ??  SJ   20:56     0:00,85 /usr/local/sbin/httpd
```
And then grep / awk it to get %CPU:

```
ps axocpu,command= | grep httpd | grep -v grep | awk '{print $1;}'
```
Insert wanted process name instead httpd

Or if you want to look by pid just

```
ps axocpu= 17732
```


----------



## Graaf_van_Vlaanderen (Dec 21, 2009)

Thanks, I used perl:


```
top -CU depaepe -b -d 50 | grep 51851 | perl -pe '$_ = substr($_, 61,8) . "\n"' | tee top_out_
```


This will create a column of fifty values, where each measurement was done with the (standard) interval of 2s.
It still not hundred percent what I want but it works. I used top, since I'm not sure how to use ps in batch mode.


----------



## Alt (Dec 21, 2009)

This is funny but i dont know how to use top in batch mode))))
ps does not have 'interactive' mode


----------



## Graaf_van_Vlaanderen (Dec 21, 2009)

Alt said:
			
		

> This is funny but i dont know how to use top in batch mode))))
> ps does not have 'interactive' mode



The "-b" option will invoke batch mode for top. From what I tried
you have to use it with the "-d" option.

If you install "topless" from the sysutils port, ps can behave
somewhat like top (gets updated). 
For example:


```
%topless ps -aux
```


----------

