Solved

Capturing information from a top script

Posted on 2004-09-02
5
452 Views
Last Modified: 2013-12-26
I am writing a script in SH and want to added to my current script to obtain this output specific area from the top command

CPU TTY   PID    USERNAME PRI  NI   SIZE     RES   STATE    TIME  %WCPU  %CPU  COMMAND
 1       ?    22557  SQL          138 39   2016K   596K  sleep      0:33   79.73      70.45  compress


There was a script that that I found on here that output for uptime this way, what changes can I make to this script
to work with top.

Here was the script I found

uptime | sed -e 's/.*: //' -e 's/ //g' | awk -F , ' { print "A="$1 "\nB=" $2 "\nC=" $3 } '


I tried using top -n 1 and it prints out the other stuff from top that is not needed
0
Comment
Question by:eaglesprings
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 11969786
top -n 1|grep '%'

0
 
LVL 48

Expert Comment

by:Tintin
ID: 11969799
Whoops.  I was working with top -n 1 output on a Solaris box which has a different format.

top -n 1|sed -n 8p

You may need to adjust the line number on the sed statement depending on how many lines top -n 1 outputs on your system.
0
 

Author Comment

by:eaglesprings
ID: 11970055
I am working on HP UX
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 100 total points
ID: 11970481
err, isnt top interactive ... since all you are interested in is one time system snapshot, why not use ps with appropriate arguments

 ps -p 1 -o "cpu longtname pid uname pri ni sz rssize state time pcpu comm"

does nearly the same thing ... the exact options may be bit different for your unix but ps might be better to use here
0
 
LVL 38

Accepted Solution

by:
yuzh earned 400 total points
ID: 11970483
For HP-UX,

you can use something like:

top -n 1 -d 1 t | sed -n 6p

the output will looks like:

Memory: 55524K (5152K) real, 111180K (4020K) virtual, 316900K free  Page# 1/147


 TTY  PID USERNAME PRI NI   SIZE    RES STATE    TIME %WCPU  %CPU COMMAND
  ?  1442 root     154 20   608K   948K sleep   14:54  0.12  0.12 dtgreet

 
Note that: if you redirect the output to a file, it is in ONE line, looks like:

Memory: 54664K (7288K) real, 110540K (7104K) virtual, 317868K free  Page# 1/145ESC[10;2HTTY  PID USERN
AME PRI NI   SIZE    RES STATE    TIME %WCPU  %CPU COMMANDESC[11;3H?  1442 rootESC[5C154 20   608K   9
48K sleep   14:54  0.09  0.09 dtgreet

You need to do a bit more work to get the result you wanted.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to match SAS datasets by multiple key variables and then rename and create new variables? 2 98
sum13 challenge 24 123
ShiftLeft challenge 21 86
x-bar in Google Sheets 2 72
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question