[ PLEASE! Do not top-post. ] [ Please remove any text not required for context. ]
On Friday 07 December 2007 12:41, [EMAIL PROTECTED] wrote: > > On Fri, 7 Dec 2007, John W.Krahn wrote: > > > > On Friday 07 December 2007 04:36, [EMAIL PROTECTED] > > wrote: > >> > >> I am not very good at perl however our systems use it and I am > >> trying to write a few scripts to help our staff. > >> > >> I am trying to calculate the average CPU utilisation from a log > >> file which is automatically generated. > > > > I would write it something like this: > > > > use strict; > > use warnings; > > > > my $file = $ARGV[ 0 ] || 'pickcpua.dat'; > > > > open my $CPUFILE, '<', $file or die "Unable to open '$file' $!"; > > > > my ( $total, $count ); > > while ( <$CPUFILE> ) { > > next unless / > > ^ > > (?: Mon | Tue | Wed | Thu | Fri | Sat | Sun ) > > \s+ > > (?: Jan | Feb | Mar | Apr | May | Jun | > > Jul | Aug | Sep | Oct | Nov | Dec ) > > \s+ > > \d\d > > \s+ > > \d\d : \d\d : \d\d > > \s+ > > ( \d+ ) > > /x; > > $total += $1; > > $count++; > > } > > > > my $average = $total / $count; > > > > print "Average CPU Time for all servers in $file file is $average\n"; > > > > __END__ > > Hi John Hello Andrew, > If you're using Linux you could use cut in a shell script, I could? How would I do that, and why would I want to? > but if > you're not then the following one liner will extract the column:- > perl -alne 'print $F[4];' <data file> And then what? How do you get the averages from that? > One thing to take care with is the supposed space delimiter betwee > the columns, The OP has shown us the data file and it sure looks like the columns are separated by whitespace. What makes you think that they are not? > it can often be a tab character. Which is a whitespace character. > Pulling the lines > into a file then looking at the file with the vim editor with set > list on will highlight them. I don't, as a rule, use vim. So, how would you do that? > There are other ways to get the cilumn > using a regular expression. Can you provide examples of these "other ways"? John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/