Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capturing information from a top script

Posted on 2004-09-02
5
Medium Priority
?
468 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 400 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 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Loops Section Overview
Suggested Courses

886 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