![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Random numbers from 0 to 1000 | wakhan | Shell Programming and Scripting | 2 | 07-15-2008 07:41 AM |
| How to set constrain on random numbers in c | ahjiefreak | High Level Programming | 6 | 01-11-2008 04:46 AM |
| how to print out line numbers of a text file? | forevercalz | Shell Programming and Scripting | 4 | 12-12-2005 05:04 AM |
| Random numbers without repetition | asal_email | UNIX for Dummies Questions & Answers | 8 | 07-14-2005 04:02 AM |
| Sed - Replacing numbers w/asteriks in random location in a file | giannicello | UNIX for Dummies Questions & Answers | 9 | 10-04-2001 12:03 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Hi one and all,
I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets received and I can't figure out how to use these numbers in a script to perform mathematical functions between them. Code:
netstat -s > /home/nistleloy/Documents/datafile | grep 'total packets received' /home/nistleloy/Documents/datafile >> /home/nistleloy/Documents/tempIP Code:
nistleloy@****:~/Documents> cat tempIP
6643 total packets received
6718 total packets received
7293 total packets received
7785 total packets received
Will I have to make each line unique so i can grep out just the actual numbers? (How would I do that with physically modifying the txt file). Any pointers and/or suggestions welcome. |
|
||||
|
Great stuff guys, thank you for that.
I hate to tap your brains again but here goes: Now that I have just the numbers displayed within the file is there anyway I can call upon the numbers in a script so I can perform the workings out on them? What i'm trying to achieve to get is the difference between the readings: 1st reading - 2nd reading 3rd reading - 4th reading but I'm not sure on how to get these random numbers into a script. I have a few thoughts: Should I turn them into variables within the file and then use the variables in my script ie edit the text file from my script and turn each number into a variable, so it'll look something like this: Code:
nistleloy@****:~/Documents> cat tempIP r1=6643 r2=6718 r3=7293 r4=7785 OR: is there a way to select line 1 from the file and make it a variable in my script then line 2 etc (remembering that these numbers are random so "greping" will not work). Regards |
|
||||
|
Quote:
Code:
COUNT=0
for num in `cat tmpfile2`
do
COUNT=`expr $COUNT + 1`
eval r${COUNT}=$num
done
echo "$r1 $r2 $r3 $r4"
|
|
||||
|
Quote:
That's perfect for my script. I've been bashing my head for a week over this! I can get on with other things now. Thanks for all replies Cheers |
|
||||
|
Hi, here's some ideas applied to Your data... assuming (including any number of space):
Code:
6643 total packets received
6718 total packets received
7293 total packets received
7785 total packets received
Code:
#!/bin/bash cnt=0 oldval=0 while read newval x y z; do echo diff=$((newval-oldval)) oldval=$newval ((cnt++)) done < tmpIP Code:
#!/bin/bash cnt=0 while read newval x y z; do [ $((cnt % 2)) -eq 1 ] && echo diff=$((newval-oldval)) oldval=$newval ((cnt++)) done < tmpIP The x y z is just to consume values from tmpIP file, otherwise the whole line would be considered one variable. The read is good for ignoring whitespace, as many other clu's are. Well, just my 2 öre! /Lakris PS Oh, almost forgot... maybe You could consider piping Your data collecting directly through the while loop, and not using intermediate files. Just a thought. |
![]() |
| Bookmarks |
| Tags |
| shell script, shell scripting, unix scripting, unix scripting basics |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|