Solved

Capturing information from a top script

Posted on 2004-09-02
5
449 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
Comment Utility
top -n 1|grep '%'

0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
Comment Utility
I am working on HP UX
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 100 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now