Retrieving random numbers out of a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Retrieving random numbers out of a text file
# 1  
Old 12-12-2008
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  
Old 12-12-2008
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  
Old 12-12-2008
try this...
Code:
netstat -s|awk -F"[\t ]" '{print $2}'

# 4  
Old 12-12-2008
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  
Old 12-12-2008
Code:
$ cat tempIP
    6643 total packets received
    6718 total packets received
    7293 total packets received
    7785 total packets received

Code:
nawk 'FNR%2 {o=$1;next} {printf("%d - %d = %d\n", o, $1, o-$1)}' tempIP

# 6  
Old 12-12-2008
Quote:
Originally Posted by nistleloy
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"

# 7  
Old 12-12-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

2. Shell Programming and Scripting

Retrieving values from a line in text file

Hi, I am new to Unix/ksh script and will like to check how do I retrieve just the count of '258' in the last line in a text file ? There will be this "TRL" appended with number of count at the last line in a text file . TRL0000000258 var=`grep 'TRL' $HOME/folder/test.txt | wc -l` ... (12 Replies)
Discussion started by: snowfrost
12 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Help in retrieving the ending line numbers of the functions

Hi! I've a C file which consist of many function definitions with numbers at the beginning as shown below.. 10 void search() 11 { 12 /*body 14 * 15 * 17 * 18 * 40 * 42 * 60 } 90 void func_name() 95 { 99 /*body 100 * 105 * 111 * (7 Replies)
Discussion started by: abk07
7 Replies

5. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

6. Shell Programming and Scripting

Random word from a flat text file

Hello, I need to take a random word from a flat text file with words in it seperated by spaces. The code I am using, always gives me the first word. Can anyone please shed some light on this. Here's my code. Thanks echo table roof ceiling jar computer monitor keyboard carpet >... (5 Replies)
Discussion started by: Freakhan
5 Replies

7. Shell Programming and Scripting

random select text file ranamed

i want to need script.. source.txt /home/user1/public_html/test3 /home/user90/public_html/test9 . . . /home/user650/public_html/test000 read source.txt and cd /home/user**/public_html/*** and there is 1.txt, 2txt ~~25.txt and select 6 text files randomly among the... (4 Replies)
Discussion started by: topic32428285
4 Replies

8. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies

9. UNIX for Dummies Questions & Answers

Sed - Replacing numbers w/asteriks in random location in a file

I have a file which contains lots of text (comment field). I would like to parse through the comment field which can be up to 255 characters long and look for anything that seems to resemble, say, a credit card number or customer account number, etc. and replace the numbers with asteriks (*). ... (9 Replies)
Discussion started by: giannicello
9 Replies
Login or Register to Ask a Question