Operating System - HP-UX
1832235 Members
2563 Online
110041 Solutions
New Discussion

Re: Again: a script help for cat top info and print in one line.

 
SOLVED
Go to solution
violin
Occasional Advisor

Again: a script help for cat top info and print in one line.

Dear All,

Yesterday I asked how to cat some lines of top info and I got many useful tips,

I did like this:
$ more top5
#!/bin/sh
> top.out
top -d 1 -f top.out
cat top.out | egrep '^Memory|^System|^avg'
$

And I added a cron job:
0,15,20,45 * * * * /tmp/top5>>/tmp/top5.log

Then review top5.log to check if cpu overloading.

The log contents are:
System: gbm2 Wed May 8 09:55:05 2002
avg 2.63 54.7% 0.0% 7.2% 38.1% 0.0% 0.0% 0.0% 0.0%
Memory: 3453692K (1200988K) real, 3217448K (1086940K) virtual, 1839072K free Page# 1/35
System: gbm2 Wed May 8 10:00:06 2002
avg 3.81 33.2% 0.0% 3.8% 63.0% 0.0% 0.0% 0.0% 0.0%
Memory: 3394480K (1175644K) real, 3186636K (1072384K) virtual, 1898448K free Page# 1/35
System: gbm2 Wed May 8 10:05:05 2002
avg 3.28 63.1% 0.0% 9.2% 27.7% 0.0% 0.0% 0.0% 0.0%
Memory: 3454928K (1239396K) real, 3224608K (1113676K) virtual, 1837860K free Page# 1/34

BUT I want to format the info in one line,
and substr some column like this:

System:gbm2 May 8 09:55:05 2002 avg 54.7% 38.1% Memory:3453692K (1200988K) real,1839072K free
System:gbm2 May 8 10:00:06 2002 avg 33.2% 63.0% Memory:3394480K (1175644K) real,1898448K free
System:gbm2 May 8 10:05:05 2002 avg 63.1% 27.7% Memory:3454928K (1239396K) real,1837860K free

I want to format in one line:
Sysem + DateTime + user% + idle% + mem real + mem free

The attachment is the top5 log and the format I want.

Thanks for help in advance.

Violin.
6 REPLIES 6
Deepak Extross
Honored Contributor
Solution

Re: Again: a script help for cat top info and print in one line.

A piece of cake using awk.
Replace
cat top.out | egrep '^Memory|^System|^avg'
with
cat top.out | egrep '^Memory|^System|^avg' | awk ' {printf $1 " " $2 " " $4 }'

Just add whatever fields you want using $x. I've only put down the 1st, 2nd and 4th fields, but you get the idea.
Deepak Extross
Honored Contributor

Re: Again: a script help for cat top info and print in one line.

Here's the entire script, just check that I've selected the right fields before you deploy this.
q=`cat top.out | egrep '^Memory|^System|^avg' `
echo $q | awk '{ printf $1" "$2" " $4 " "$5" " $6" "$7" " $8 " " $10" " $13" "$18" "$19" "$20" "$21" "$25" "$26" \n"}'

By the way, you dont seem to have assigned points for your last post...
violin
Occasional Advisor

Re: Again: a script help for cat top info and print in one line.

Sorry for forgot assign points.

THANK YOU SO MUCH MUCH MUCH MUCH MUCH!!

Today is my day and I'm so happpppy!
Niraj Kumar Verma
Trusted Contributor

Re: Again: a script help for cat top info and print in one line.

#!/bin/sh

top -d 1 -f top.out

Host_Name=`cat top.out|grep ^System |awk '{print $2}'`

Time=`cat top.out|grep ^System |awk '{print $3"-"$5"-"$4"-"$6}'`

Memory_Real=`cat top.out |grep ^Memory|awk '{print $2 $3}'`

Memory_Free=`cat top.out |grep ^Memory|awk '{print $8}'`

Load_avg_User=`cat top.out |grep ^avg|awk '{print $3}'`

Load_avg_Idle=`cat top.out |grep ^avg|awk '{print $6}'`

echo "System: $Host_Name $Time Avg: $Load_avg_User $Load_avg_Idle Memory: $Memory_Real(Real) $Memory_Free(Free)"

#EOS

-Niraj
Niraj.Verma@philips.com
Niraj Kumar Verma
Trusted Contributor

Re: Again: a script help for cat top info and print in one line.

Hey !!

stiill you have n't assigned points to your previous query !!

::::))))
Niraj.Verma@philips.com
violin
Occasional Advisor

Re: Again: a script help for cat top info and print in one line.

Sorry,I'm a newbie for this group,
I don't know it's a manner to assign points for reply.

I've assigned points for all replies.
Thanks for your helps again.

Violin.