LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   little calculation in script. (https://www.linuxquestions.org/questions/linux-newbie-8/little-calculation-in-script-820651/)

zirias 07-19-2010 02:12 PM

Ok, i admit, i never learned awk ... seems quite powerful, too

Tinkster 07-19-2010 03:59 PM

Quote:

Originally Posted by zirias (Post 4038469)
Ok, i admit, i never learned awk ... seems quite powerful, too

perls little brother, if you wish ... ;}

pinga123 07-20-2010 02:06 AM

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


GrapefruiTgirl 07-20-2010 05:04 AM

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 :D but it's up to you.

Cheers!

pinga123 07-20-2010 06:17 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 4039270)
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 :D but it's up to you.

Cheers!

Thanks for your help.


All times are GMT -5. The time now is 02:41 AM.