Operating System - HP-UX
1828317 Members
4928 Online
109976 Solutions
New Discussion

Re: How to collect data from command output

 
SOLVED
Go to solution
Gary L
Super Advisor

How to collect data from command output

Hi

I wanna collect some data from "top" output for writing a Unix system capacity report script. As we know, #top -d 1 will list many contents and datas, but I just wanna collect the datas from line 1 to line 11, I don't wanna get detail process CPU TTY PID .. informations, How do i do?
I know, cut -c1-11 just for columns 1 to 11 not row

thanks
9 REPLIES 9
Patrick Wallek
Honored Contributor
Solution

Re: How to collect data from command output

How about a 2 step process:

# top -d 1 -f /var/tmp/top-file
# head -11 /var/tmp/top-file > /var/tmp/top-file-11

Gary L
Super Advisor

Re: How to collect data from command output

Hi Patrick

Thank you very much for your fast reply. It works according as your "head" command. I have no idea why I forgot head and tail command.
one more question:
Is there any command could collect specified row, such as 2-11 or 2-5,11-15,22 etc.

thanks
Gary L
Super Advisor

Re: How to collect data from command output

As we know, "cut" have this option: cut -c1-3,8
spex
Honored Contributor

Re: How to collect data from command output

Hello,

It's likely that 'sar' can give you the metrics you're interested in directly.

For example,

$ sar -u 5 12
will report CPU utilization every 5 seconds for 1 minute

$ sar -b 5 12
will report buffer activity

$ sar -d 5 12
will report disk activity

...and so on.

Note that the "system activity report package" can be used to automatically capture system data in the background. It will also generate detailed reports.

See sar(1M) and sa1(1M) for more information.

PCS
Patrick Wallek
Honored Contributor

Re: How to collect data from command output

To print specific lines of a file you can use sed:

to print lines 11-15:

# sed -n '11,15p' /var/tmp/top-file

If you want to redirect that output to another file:

# sed -n '11,15p' /var/tmp/top-file > /var/tmp/top-file-11-15

If you want a single line:

# sed -n '11p' /var/tmp/top-file

These sed examples came from:

HANDY ONE-LINERS FOR SED
http://sed.sourceforge.net/sed1line.txt
A. Clay Stephenson
Acclaimed Contributor

Re: How to collect data from command output

It's a bit short-sighted to hardcode the number of lines because the top output varies with the number of CPU's. A smarter approach would be to print until you see the "CPU TTY" line:

# top -d 1 -f /var/tmp/top-file
# head -11 /var/tmp/top-file > /var/tmp/top-file-11

#!/usr/bin/sh

typeset TDIR=${TMPDIR:-/var/tmp}
typeset T1=${TDIR}/X${$}_1.out

trap 'eval rm -f ${T1}' 0 1 2 3 15
top -d 1 -f ${T1}
awk '/^CPU TTY/ {exit}; {print $0}' ${T1}


If it ain't broke, I can fix that.
spex
Honored Contributor

Re: How to collect data from command output

Hello,

Print rows 2-5, 11-15, and 22:
$ sed -n '2,5p;11,15p;22p' < infile

PCS
Gary L
Super Advisor

Re: How to collect data from command output

Hi Spex

thanks for your reply.
Yes, "sar" is a very useful command. it could list many kinds of data, like cpu, disk, buffer etc. even I could use -A option like all. But except -u cpu output info is very simple and clear other info hard to read. As you may know, this system report just for a biz manager, all contents should simple and easy to read. So through compare I found "top" output is better, have cpu and mem.

sa1 and sa2 is a simple and good scripts.

thank
Gary L
Super Advisor

Re: How to collect data from command output

Hi Spex, Patrick and A. Clay Stephenson

Thank you very much for your kindly helps,
"sed" command is very useful, Clay's awk CPU TTY script is very useful too. those two ways give me more idea and suggestions.

thanks again.

Have a great day and take care, buddies.