Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
read and sum values from a textfile using the bash
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
zbled
Apprentice
Apprentice


Joined: 18 Jun 2002
Posts: 216
Location: Bukowski's Piano Bar

PostPosted: Sat Oct 05, 2002 7:42 pm    Post subject: read and sum values from a textfile using the bash Reply with quote

hi everybody, i'm pretty new to bash things, so i hope you can help me:

due to my provider i have to watch my upload and download volume. I want to build a little script, which should show the amount of bytes up and downloaded for a month:

to get the actual down- and upload written in a textfile when shutting down the workstation, i'm going to execute the following commands:

/sbin/ifconfig eth0 | grep "RX bytes" | gawk '{print $6}' | gawk -F':' '{print $2}' >> updown
/sbin/ifconfig eth0 | grep "RX bytes" | gawk '{print $2}' | gawk -F':' '{print $2}' >> updown

so far's everything okay, but i don't know how to read and sum the values written in the file updown. is there any bash command to open the file and sum the values till the EOF?

thanks in advance
Back to top
View user's profile Send private message
nikai
Apprentice
Apprentice


Joined: 02 Oct 2002
Posts: 270
Location: Kitzbühel, Austria

PostPosted: Sat Oct 05, 2002 10:28 pm    Post subject: Re: read and sum values from a textfile using the bash Reply with quote

zbled wrote:
i don't know how to read and sum the values written in the file updown. is there any bash command to open the file and sum the values till the EOF?


Hm, if I understand correctly you want something like

Code:
awk '{print x+=$1}' updown


Is that right?
Back to top
View user's profile Send private message
sa
Guru
Guru


Joined: 10 Jun 2002
Posts: 450

PostPosted: Sat Oct 05, 2002 11:13 pm    Post subject: Reply with quote

something like this?
Code:

#!/bin/bash
file=~/updown
j=1
n=`cat $file | wc | awk '{print $1}'`
until [ $j -gt $n ]
do
    total=$(( $total + `head -$j $file | tail -1`))
    j=$(( $j + 1))
done
echo $total


you might want to check out:http://people.ethz.ch/~oetiker/webtools/mrtg/
not sure if its what you need, but its pretty neat.
Back to top
View user's profile Send private message
sa
Guru
Guru


Joined: 10 Jun 2002
Posts: 450

PostPosted: Sat Oct 05, 2002 11:15 pm    Post subject: Reply with quote

ooo, that awk thing is pretty cool :-)
Back to top
View user's profile Send private message
zbled
Apprentice
Apprentice


Joined: 18 Jun 2002
Posts: 216
Location: Bukowski's Piano Bar

PostPosted: Sat Oct 05, 2002 11:36 pm    Post subject: Reply with quote

yep, that's what i wanted, but this code is much quicker than the loop ;)

Quote:
sum=0; for i in `cat /usr/local/save/updown`; do ((sum+=i));done; sum=$[$sum/1048576]


anyways, there's still one problem left. when calculating these values (+), the sum becomes negativ

164377428
+340828451
+164377488
+340828573
+164377488
+340828573
+164377548
+340828635
+164377643
+340828762
+164377769
+340829127

Quote:
zbled@heisl zbled $ sum=0; for i in `cat /usr/local/save/updown`; do ((sum+=i));done; sum=$[$sum/1048576]; echo $sum
-1205


can you still help me?
Back to top
View user's profile Send private message
sa
Guru
Guru


Joined: 10 Jun 2002
Posts: 450

PostPosted: Sun Oct 06, 2002 12:33 am    Post subject: Reply with quote

just tried that in my shell, zsh and it works properly mabey bash is broken?
you could do as nikai suggests:
Code:

sa@g ~ % total=`awk '{print x+=$1/1048576}' updown`
sa@g ~ % echo $total                               
156.763
481.802
638.564
963.604
1120.37
1445.41
1602.17
1927.21
2083.97
2409.01
2565.77
2890.81


but then you have to extract the last word from $total
good luck,
sa
Back to top
View user's profile Send private message
zbled
Apprentice
Apprentice


Joined: 18 Jun 2002
Posts: 216
Location: Bukowski's Piano Bar

PostPosted: Sun Oct 06, 2002 12:44 am    Post subject: Reply with quote

are there any switches for the bash, cause mine seems to be restricted to a range between - 2000 to 2000?

[edit] i just emerged zsh and i'm getting the same values :(... seems that it's nothing to do with the shell [/edit]
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum