Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search file for integer values.

Status
Not open for further replies.
Apr 22, 2008
10
IE
Hi
I have to search a file for a floating point number.

"Summary: total flows: 1027, total bytes: {{9.2}} M, total packets: 18251, avg bps: 259145, avg pps: 60, avg bpp: 531"

The number is between the double chain brackets in the above string.The number is not always the same and this string can be repeated a number of times in a file.How do i get this number ?

 
A regular expression sounds like the best bet here. Are you familiar with perl or regular expressions at all?



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have been look at regex today. I can get my head arounf them but I think I am having problems with the blank space after "bytes:__". Here is my code.
-------code------------------------------------
#!/usr/bin/perl
$X=[0..100];
$Y=[0..100];
$search="total bytes: $X.$Y M,";
open(INFO, "Port25_05_03");
@array=<INFO>;
close (INFO);
foreach $line (@array){
if ($line =~ /$search/)
{
print "Found\n";
$var = substr(12,3);
print"$var\n";
}

}
 
$X and $Y are references to arrays, they could not be used to search for a pattern like you are trying. Do this:

Code:
#!/usr/bin/perl
$X=[0..100];
$Y=[0..100];
    $search="total bytes: $X.$Y M,";
print $search;

and see what the value of $search is.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm a bit confused by the arrays, here. I'd probably go with something like
Code:
use strict;
use warnings;

while (<>) {
   print "$1\n" if (/total bytes: (\d+\.\d+ \w)/);
}
(not tested)

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hey,
I tried stevexff suggestion and it worked.Here is my working code.

#!/usr/bin/perl
use strict;
use warnings;
open(INFO, "Port25_05_03");
while (<INFO>){

print"$1\n" if (/totalbytes:(\d+\.\d+\w)/);

}
close(INFO);
 
Oh good. I decided to include the 'M' as you might find that for very large or very small files this gets listed as K, G, or even T. You might have to mess about with the parsing depending on how variable the input formatting is. Currently this is searching for 'total bytes:' followed by a space, one or more digits, a decimal point, one or more digits, a space, and a character. So for example this won't find 2 M but it will find 33.0 K

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I am testing it now and it is working fine.
So thanks again...

Another issue (I probably should start a new thread...) Do you know a way I can increment the hour in this system call?

-> "09" is the start hour and i want to call system() at 5 mins intervals until 23:59:59

for($hour="09";$hour<"23:59:59";$hour++)
{
system("nfdump -R $dir -t $year/$month/$day.$Hour:05:00-$year/$month/$day.$Hour:10:00");
}

Any ideas?
 
You are on a *nix box, judging by the shebang line in your Perl. Check out using cron to schedule tasks. Failing that, look at Perl's sleep function.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Cheers Steve..

I am having a look now...Question: I did a search for C/C++ on tek-tips nothing came back....is there a section?
 
There are a number of them, focused on particular dialects. Go here and choose the ones you like...


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top