Sponsored Content
Full Discussion: count average
Top Forums Shell Programming and Scripting count average Post 302199011 by in2nix4life on Sunday 25th of May 2008 04:55:52 PM
Old 05-25-2008
Hope this helps set you in the right direction:

#!/usr/bin/env perl

use strict;

# define the file
my $file = "in-file.txt";

# open the file and dump it's contents into an array
open(FILE, "<$file") or die "Unable to open $file: $!\n";
my @FILE_IN = <FILE>;
close(FILE);

# define a couple of variables we'll need
my $total;
my $average;

# loop through the file and parse out the name, id, and the average
foreach(@FILE_IN) {
chomp;
if ($_ =~ /^([0-9]+).(\w+\s\w+).(.*)/) {
print "Name: $2\n";
print "ID#: $1\n";
my @MARKS = split('\s', $3);
my $num_of_marks = $#MARKS + 1;

foreach(@MARKS) {
$total += $_;
}

my $average = $total / $num_of_marks;
# adjust %.1f to the desired number of decimal places
printf "Avg. mark: %.1f\n\n", $average;

# reset the total and average variables
# otherwise they'll be carried over
# to the next run through the loop
$total = 0;
$average = 0;
} else {
print "There was a problem!\n";
exit(1);
}
}

exit(0);
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

count average

Hi Friends, Can any one help me with count average of student marks in this file (i can not change structure of the input file): input file: 1 - student ID 2 - student name 3 - group ID 4 - teacher ID 5 - marks (numbers of marks are different) 1:John Smith:2:3:2 3 4 5 2:Mark... (1 Reply)
Discussion started by: mleplawy
1 Replies

2. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

3. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

4. Shell Programming and Scripting

Count time min/max/average for ping

I am redirecting my ping output to a file. The sample output is like this: 64 bytes from xx.xx.xx.167: icmp_seq=4490 ttl=116 3.75 ms 2011Jul12- 15 40 16 64 bytes from xx.xx.xx.167: icmp_seq=4491 ttl=116 5.29 ms 2011Jul12- 15 40 17 64 bytes from xx.xx.xx.167: icmp_seq=4492 ttl=116 4.88 ms... (6 Replies)
Discussion started by: zorrox
6 Replies

5. Shell Programming and Scripting

How to find the average,min,max ,total count?

Hi , Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file. A , B ,C ,D ,E ,F,G ,H 4141,127337,24,15,20,69,72.0,-3 4141,128864,24,15,20,65,66.0,-1 4141,910053,24,15,4,4,5.0,-1 4141,910383,24,15,22,3,4.0,-1 4141,496969,24,15,14,6,-24.0,-18... (7 Replies)
Discussion started by: vinothsekark
7 Replies

6. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

7. UNIX for Beginners Questions & Answers

How to count average and max respon time?

sorry i will revise first (1 Reply)
Discussion started by: fajar_3t3
1 Replies

8. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

9. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies
Regexp::RegGrp(3pm)					User Contributed Perl Documentation				       Regexp::RegGrp(3pm)

NAME
Regexp::RegGrp - Groups a regular expressions collection VERSION
Version 1.002 DESCRIPTION
Groups regular expressions to one regular expression SYNOPSIS
use Regexp::RegGrp; my $reggrp = Regexp::RegGrp->new( { reggrp => [ { regexp => '%name%', replacement => 'John Doe', modifier => $modifier }, { regexp => '%company%', replacement => 'ACME', modifier => $modifier } ], restore_pattern => $restore_pattern } ); $reggrp->exec( $scalar ); To return a scalar without changing the input simply use (e.g. example 2): my $ret = $reggrp->exec( $scalar ); The first argument must be a hashref. The keys are: reggrp (required) Arrayref of hashrefs. The keys of each hashref are: regexp (required) A regular expression replacement (optional) Scalar or sub. A replacement for the regular expression match. If not set, nothing will be replaced except "store" is set. In this case the match is replaced by something like sprintf("x01%dx01", $idx) where $idx is the index of the stored element in the store_data arrayref. If "store" is set the default is: sub { return sprintf( "x01%dx01", $_[0]->{store_index} ); } If a custom restore_pattern is passed to to constructor you MUST also define a replacement. Otherwise it is undefined. If you define a subroutine as replacement an hashref is passed to this subroutine. This hashref has four keys: match Scalar. The match of the regular expression. submatches Arrayref of submatches. store_index The next index. You need this if you want to create a placeholder and store the replacement in the $self->{store_data} arrayref. opts Hashref of custom options. modifier (optional) Scalar. The default is 'sm'. store (optional) Scalar or sub. If you define a subroutine an hashref is passed to this subroutine. This hashref has three keys: match Scalar. The match of the regular expression. submatches Arrayref of submatches. opts Hashref of custom options. A replacement for the regular expression match. It will not replace the match directly. The replacement will be stored in the $self->{store_data} arrayref. The placeholders in the text can easily be rereplaced with the restore_stored method later. restore_pattern (optional) Scalar or Regexp object. The default restore pattern is qr~x01(d+)x01~ This means, if you use the restore_stored method it is looking for x010x01, x011x01, ... and replaces the matches with $self->{store_data}->[0], $self->{store_data}->[1], ... EXAMPLES
Example 1 Common usage. #!/usr/bin/perl use strict; use warnings; use Regexp::RegGrp; my $reggrp = Regexp::RegGrp->new( { reggrp => [ { regexp => '%name%', replacement => 'John Doe' }, { regexp => '%company%', replacement => 'ACME' } ] } ); open( INFILE, 'unprocessed.txt' ); open( OUTFILE, '>processed.txt' ); my $txt = join( '', <INFILE> ); $reggrp->exec( $txt ); print OUTFILE $txt; close(INFILE); close(OUTFILE); Example 2 A scalar is requested by the context. The input will remain unchanged. #!/usr/bin/perl use strict; use warnings; use Regexp::RegGrp; my $reggrp = Regexp::RegGrp->new( { reggrp => [ { regexp => '%name%', replacement => 'John Doe' }, { regexp => '%company%', replacement => 'ACME' } ] } ); open( INFILE, 'unprocessed.txt' ); open( OUTFILE, '>processed.txt' ); my $unprocessed = join( '', <INFILE> ); my $processed = $reggrp->exec( $unprocessed ); print OUTFILE $processed; close(INFILE); close(OUTFILE); AUTHOR
Merten Falk, "<nevesenin at cpan.org>" BUGS
Please report any bugs or feature requests through the web interface at http://github.com/nevesenin/regexp-reggrp-perl/issues <http://github.com/nevesenin/regexp-reggrp-perl/issues>. SUPPORT
You can find documentation for this module with the perldoc command. perldoc Regexp::RegGrp COPYRIGHT &; LICENSE Copyright 2010, 2011 Merten Falk, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-02-18 Regexp::RegGrp(3pm)
All times are GMT -4. The time now is 11:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy