LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-19-2010, 03:12 PM   #16
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59

Ok, i admit, i never learned awk ... seems quite powerful, too
 
Old 07-19-2010, 04:59 PM   #17
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by zirias View Post
Ok, i admit, i never learned awk ... seems quite powerful, too
perls little brother, if you wish ... ;}
 
Old 07-20-2010, 03:06 AM   #18
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
I have started developing the script as said.

This is how my script looks.
it takes the output of free -m and insert into a file for given amount of time.

Code:
#!/bin/bash

echo "Enter Time Interval"
read inputtime

rm -rf /tmp/raminfo
i=0
echo "RAM Information for last $inputtime Seconds" > /tmp/raminfo
while [ $i -le $inputtime ]
do
free -m | grep Mem >> /tmp/raminfo
sleep 1
i=$( expr $i + 1 )
done
Output.
Code:
# ./test
Enter Time Interval
10
# cat /tmp/raminfo
RAM Information for last 10 Seconds
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
Mem:           503        468         34          0         58        260
How would i create and attach new column indicating RAM utilization.
This can be done by taking col3/col2 but i m getting following error.

Code:
# cat /tmp/raminfo | awk '{print $3/$2}'
awk: (FILENAME=- FNR=1) fatal: division by zero attempted
 
Old 07-20-2010, 06:04 AM   #19
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
1) The error is probably coming from this line:

Code:
RAM Information for last 10 Seconds
Which is the first line of the file, and which AWK is interpreting (or trying to) as numbers, which it is not, hence division by zero. You need a method of skipping over that first line of the file, or selecting only the lines which actually have the numerical data that you want. Also, AWK can read files all by itself, so you don't really need that `cat` there. I would try something like below, which places a simple regexp (regular expression) into the awk, which will match only the lines beginning with "Mem:" and ignore the rest:

Code:
awk '/^Mem:/ { print $3 / $2}' /tmp/raminfo
2) I'm not sure if the desired output you want from this is a XX/YY format, or if you actually planned to do that division operation $3/$2, but if you want the division thing, you may wish to multiply the result by 100 to get a percent, which would be maybe more pleasing to the eye. OR, if you just want a display of XX/YY then you'll need to put the / into "double quotes".

3) Finally a suggestion/idea which is maybe just a matter of preference for me: when doing simple shell math, you could use i=$((i+1)) instead of i=$( expr $i + 1 ) if for no other reason than that there are three less characters to type but it's up to you.

Cheers!
 
Old 07-20-2010, 07:17 AM   #20
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by GrapefruiTgirl View Post
1) The error is probably coming from this line:

Code:
RAM Information for last 10 Seconds
Which is the first line of the file, and which AWK is interpreting (or trying to) as numbers, which it is not, hence division by zero. You need a method of skipping over that first line of the file, or selecting only the lines which actually have the numerical data that you want. Also, AWK can read files all by itself, so you don't really need that `cat` there. I would try something like below, which places a simple regexp (regular expression) into the awk, which will match only the lines beginning with "Mem:" and ignore the rest:

Code:
awk '/^Mem:/ { print $3 / $2}' /tmp/raminfo
2) I'm not sure if the desired output you want from this is a XX/YY format, or if you actually planned to do that division operation $3/$2, but if you want the division thing, you may wish to multiply the result by 100 to get a percent, which would be maybe more pleasing to the eye. OR, if you just want a display of XX/YY then you'll need to put the / into "double quotes".

3) Finally a suggestion/idea which is maybe just a matter of preference for me: when doing simple shell math, you could use i=$((i+1)) instead of i=$( expr $i + 1 ) if for no other reason than that there are three less characters to type but it's up to you.

Cheers!
Thanks for your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory calculation mario.almeida Linux - Server 3 07-22-2010 11:05 AM
[SOLVED] complex math calculation in shell script needed gary_in_springhill Programming 33 03-20-2010 06:47 AM
date calculation satish.res Linux - Newbie 3 12-27-2009 06:31 AM
Calculation aid script! assasukasse Programming 24 09-15-2007 01:07 PM
c math calculation alaios Programming 3 06-01-2004 02:46 AM


All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration