The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-12-2008
nistleloy nistleloy is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 14
Question Retrieving random numbers out of a text file

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
These are the numbers I've obtained:
Code:
nistleloy@****:~/Documents> cat tempIP
    6643 total packets received
    6718 total packets received
    7293 total packets received
    7785 total packets received
So with these numbers I want to use them in a script that subtracts the first reading from the second and the third reading from the fourth - bearing in mind that these numbers could be any size, any length everytime I run the netstat -s cmd.

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.
  #2 (permalink)  
Old 12-12-2008
benefactr benefactr is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 71
Try this on when catting your tmpfile. This would only work if there is spaces before the numbers in your tmpfile, as it looks like there are.

Code:
cat tempIP | tr -s " " | cut -d " " -f2
  #3 (permalink)  
Old 12-12-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
try this...
Code:
netstat -s|awk -F"[\t ]" '{print $2}'
  #4 (permalink)  
Old 12-12-2008
nistleloy nistleloy is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 14
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
then call upon them in my script to do the maths - how would I assign the varibles within my script and how do I get these variables into my script?

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
  #5 (permalink)  
Old 12-12-2008
benefactr benefactr is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 71
Quote:
Originally Posted by nistleloy View Post
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
then call upon them in my script to do the maths - how would I assign the varibles within my script and how do I get these variables into my script?

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
If you get the numbers by themselves in a file, you can do the below to set them to variables.
Code:
COUNT=0
for num in `cat tmpfile2`
do
  COUNT=`expr $COUNT + 1`
  eval r${COUNT}=$num
done

echo "$r1 $r2 $r3 $r4"
  #6 (permalink)  
Old 12-12-2008
nistleloy nistleloy is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 14
Quote:
COUNT=0
for num in `cat tmpfile2`
do
COUNT=`expr $COUNT + 1`
eval r${COUNT}=$num
done
echo "$r1 $r2 $r3 $r4"
Thank you.

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
  #7 (permalink)  
Old 12-12-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
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
The first example is a method I use a lot in different versions, basically presenting the diff in a value from it's previous value. Good for analysing data from log files for example. And since there is always a beginning for everything, I chose in this case, 0:

Code:
#!/bin/bash
cnt=0
oldval=0
while read newval x y z; do
	echo diff=$((newval-oldval))
	oldval=$newval
	((cnt++))
done < tmpIP
And if You want only diff from every other line (if this is what You meant):

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
Try changing [ $((cnt % 2)) -eq 1 ] for a different result. It's a construct to return true if You are on an even line. Well, er... yes, it's uneven with regard to cnt, but we usually we start counting from 0, so, oh, whatever... fiddle about with it! Change -eq to 0 for a different, "shifted", result.
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.
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0