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
# 1  
Old 09-05-2011
Calculate average from CSV file using PERL script

Hi All

I have this csv file and I need to calculate the average of FPS.

FPS:27.7420, Interval:1314184238772
FPS:25.9798, Interval:1314184242646
FPS:27.4772, Interval:1314184246311
FPS:26.1623, Interval:1314184250159
FPS:26.4515, Interval:1314184253972
FPS:31.5896, Interval:1314184257163
FPS:28.3603, Interval:1314184260721
FPS:35.1411, Interval:1314184263592
FPS:29.7366, Interval:1314184266988
FPS:31.6124, Interval:1314184270175
FPS:30.0630, Interval:1314184273526
FPS:30.0121, Interval:1314184276884
FPS:30.1730, Interval:1314184280225
FPS:32.0201, Interval:1314184283374
FPS:33.6685, Interval:1314184286370
FPS:29.6085, Interval:1314184289923
FPS:28.7976, Interval:1314184293421
FPS:28.4840, Interval:1314184296959
FPS:31.9299, Interval:1314184300116
FPS:33.3468, Interval:1314184303149
FPS:26.6828, Interval:1314184306921
FPS:28.3489, Interval:1314184310474
FPS:31.2881, Interval:1314184313696
FPS:27.0431, Interval:1314184317593
FPS:33.4533, Interval:1314184320608
FPS:32.1011, Interval:1314184323749
FPS:25.5846, Interval:1314184327682
FPS:33.6376, Interval:1314184330681
FPS:33.4556, Interval:1314184333703
FPS:28.3385, Interval:1314184337256
FPS:35.1694, Interval:1314184340127
FPS:27.0235, Interval:1314184343855
FPS:31.5202, Interval:1314184347054
FPS:31.5341, Interval:1314184350251
FPS:32.8578, Interval:1314184353322

Anyone know how to solve it by using perl script?
# 2  
Old 09-05-2011
In awk ..
Code:
$ nawk -F"[:,]" '{print $2|"xargs"}' infile | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 09-05-2011
Hi jayan_jay

Thanks for your help. Let say my csv file name is fps.csv

should I open or declare fps.csv before use your code?
Code:
$ nawk -F"[:,]" '{print $2|"xargs"}' infile | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'


Last edited by Franklin52; 09-05-2011 at 06:29 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 09-05-2011
Corrected the filename and now try with the below command
Code:
$ nawk -F"[:,]" '{print $2|"xargs"}' fps.csv | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'

This User Gave Thanks to jayan_jay For This Post:
# 5  
Old 09-05-2011
I try the code and name the perl program as ami.pl . Im running this perl script on windows 7 and I already installed active perl on my pc. Seems like nothing happened in csv file.

---------- Post updated at 03:15 AM ---------- Previous update was at 03:10 AM ----------

Is there any conflict or problem if I run the given code on windows?
# 6  
Old 09-05-2011
read about the split function in the perl

the above posted code will not work in windows.

---------- Post updated at 02:11 PM ---------- Previous update was at 02:05 PM ----------

Code:
 
$ perl -e 'my $total=0; while (<>) { chomp; my $value = (split /([,:])/)[2]; $total=$value+$total; }print $total/$.' fps.csv
30.1827114285714

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 09-05-2011
Halo itkamaraj

I try to run this code (ami.pl)
Code:
$ perl -e 'my $total=0; while (<>) { chomp; my $value = (split /([,:])/)[2]; $total=$value+$total; }print $total/$.' fps.csv

but still can't work. I place my fps.csv and ami.pl in one folder at my dekstop. Once I click ami.pl it will just pop out and nothing happened in fps.csv. I try to add break; at the end of the code but still the .pl file close automtically.

Last edited by Franklin52; 09-05-2011 at 06:29 AM.. Reason: Please use code tags for code and data samples, thank you
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