Calculate average from CSV file using PERL script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate average from CSV file using PERL script
# 15  
Old 09-05-2011
Hye getmmg

C:\Users\mhamzah2\Desktop\media decode project>perl -F':|,' -lane '$sum+=$F[1];$
count++}{print $sum/$count' fps.csv.
''' is not recognized as an internal or external command,
operable program or batch file.

I'm using windows 7.

Quote:
Originally Posted by getmmg
Can you try this

Code:
perl -F':|,' -lane '$sum+=$F[1];$count++}{print $sum/$count' fps.csv.

---------- Post updated at 05:04 AM ---------- Previous update was at 04:54 AM ----------

Itkamaraj

Any idea why I cannot execute my pl command using pl script? How to do this operation in many csv file ex: fps1.csv, fps2. csv and etc..and the average result will be also written in each csv file after the command being execute.
# 16  
Old 09-05-2011
Quote:
Originally Posted by sayachop
I'm using windows 7.

Didn't know that. Try this, Never done one liners in windows though.
This might work.

Code:
 
 perl -F':|,' -lane "'$sum+=$F[1];}{print $sum/$.'" temp1

# 17  
Old 09-05-2011
Hi

Anybody can look what is the problem with this code.

perl -e "my $total=0; while (<>) { chomp; my $value = (split /([,:])/)[2]; $total=$value+$total; open FILE, ">", "ami.txt" or die $! }print FILE $total/$." fps.csv

I got perl -e The system cannot find the patch specified
# 18  
Old 09-05-2011
Quote:
Originally Posted by sayachop
Hi

Anybody can look what is the problem with this code.

perl -e "my $total=0; while (<>) { chomp; my $value = (split /([,:])/)[2]; $total=$value+$total; open FILE, ">", "ami.txt" or die $! }print FILE $total/$." fps.csv

I got perl -e The system cannot find the patch specified
What does the following command return in your system?

Code:
dir fps.csv

tyler_durden
# 19  
Old 09-05-2011
Volume in drive C is OSDisk
Volume Serial Number is 4023-C497

Directory of C:\Users\mhamzah2\Desktop\media decode project

09/06/2011 09:24 AM 1,794 fps.csv
1 File(s) 1,794 bytes
0 Dir(s) 130,904,260,608 bytes free
# 20  
Old 09-05-2011
Well, it works for me (Windows XP):
Code:
C:\work>perl -F: -anE "$total += $F[1]; END { say $total/$. }" fps.csv
30.1827114285714

# 21  
Old 09-06-2011
Ok, open your favorite text editor, type in this program and save the file as "fps.pl" in the same directory as "fps.csv":

Code:
#!perl -w
my $total   = 0;
my $lines   = 0;
my $infile  = "fps.csv";
my $outfile = "out.txt";

# read $infile and process it
open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
while (<IN>) {
  next if /^\s*$/;                  # ignore blank lines or lines that have whitespaces only
  chomp;                            # remove EOL character
  $total += (split /[,:]/)[1];      # running total of the FPS value
  $lines++;                         # increment the line number
}
close (IN) or die "Can't close $infile: $!";

# now write to $outfile
open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
print OUT $total/$lines;            # average is the total of FPS values divided by linecount
close (OUT) or die "Can't close $outfile: $!";

The inline comments should be self-explanatory.
Once you are at the DOS prompt after saving the Perl program "fps.pl", the "type" command should show something like this:

Code:
C:\>
C:\>
C:\>type fps.pl
#!perl -w
my $total   = 0;
my $lines   = 0;
my $infile  = "fps.csv";
my $outfile = "out.txt";

# read $infile and process it
open (IN, "<", $infile) or die "Can't open $infile for reading: $!";
while (<IN>) {
  next if /^\s*$/;                  # ignore blank lines or lines that have whitespaces only
  chomp;                            # remove EOL character
  $total += (split /[,:]/)[1];      # running total of the FPS value
  $lines++;                         # increment the line number
}
close (IN) or die "Can't close $infile: $!";

# now write to $outfile
open (OUT, ">", $outfile) or die "Can't open $outfile for writing: $!";
print OUT $total/$lines;            # average is the total of FPS values divided by linecount
close (OUT) or die "Can't close $outfile: $!";

C:\>
C:\>

Now feed the Perl program to your perl interpreter like so:

Code:
C:\>
C:\>perl fps.pl

C:\>
C:\>

Note that no output is displayed in the DOS window. The control simply returns to the next prompt.
However, the output file called "out.txt" should've been created in the same directory as "fps.csv" and "fps.pl".
To display the contents of "out.txt", use the DOS "type" command:

Code:
C:\>
C:\>type out.txt
30.1827114285714
C:\>
C:\>

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to calculate average of csv using shell scripting?

Hi, I need to calculate the average of the following values using shell scripitng. Can anyone please suggest a solution? ... (10 Replies)
Discussion started by: karan pratap si
10 Replies

2. Shell Programming and Scripting

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

3. Shell Programming and Scripting

How to calculate average of two columns and copy into another file?

Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file. Input files are: File-1: 100 99 20 99 50 99 50 99 File-2: 200 85... (3 Replies)
Discussion started by: Lokaps
3 Replies

4. Shell Programming and Scripting

Perl script to modify csv file

Hi Friends, I want to convert a csv file into a ordinary .txt file. I am able to convert but I want the output to look as shown below in the .txt file table findhost= { {"xyz","abc"}, {"rxz","mmz"}, {"vrr","nnz"}, } default={"NONE"} My current perl script #!/usr/bin/env perl... (12 Replies)
Discussion started by: dbashyam
12 Replies

5. UNIX Desktop Questions & Answers

Calculate average for rows in a text file

Dear Gurus, I have tab-delimited text files with matrix containing values. The first column is a identifier and other columns have the corresponding values. I would like to calculate the average value (total number/number of entries) for all entries from 2nd column to the last column in row... (3 Replies)
Discussion started by: Unilearn
3 Replies

6. Shell Programming and Scripting

Calculate average time using a script

Hello, I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second). Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well): 07:55:31 09:42:00 08:09:02 09:15:23 09:27:45 09:49:26... (4 Replies)
Discussion started by: jaredhanks
4 Replies

7. Shell Programming and Scripting

Please do help: Perl Script to pull out rows from a CSV file

I have CSV file that contains data in the format as shown below: ABC, 67, 56, 67, 78, 89, 76, 55 PDR, 85, 83, 83, 72, 82, 89, 83 MPG, 86, 53, 54, 65, 23, 54, 75 .. .. .. .. I want to create a script that will pull out the rows from the above sheet and paste it into another CSV file.... (12 Replies)
Discussion started by: pankajusc
12 Replies

8. Shell Programming and Scripting

Calculate average of each of position of contents in a huge file

My input: >AAA_100 10 20 50 60 10 100 15 10 >AAA_100 20 20 50 60 20 100 15 10 >AAA_100 10 20 50 60 40 100 15 10 >AAA_100 40 20 50 60 10 100 15 10 . . . My Output 20 20 50 60 20 100 15 10 If I have a long list of file. I want to calculate average of each position inside the contents... (7 Replies)
Discussion started by: patrick87
7 Replies

9. Shell Programming and Scripting

Simple Script needed for Processing CSV file perl

I am new to perl, and need a script to pull a CSV file, strip out 2 leading columns, and 2 ending columns, and resave the file in a new location. The file is basic and has less than 10,000 records. Secondly, can I schedule perl scripts to run daily? Can someone provide the basic script to... (1 Reply)
Discussion started by: cobbjob
1 Replies

10. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question