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?

In awk ..

$ nawk -F"[:,]" '{print $2|"xargs"}' infile | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'
1 Like

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?

$ nawk -F"[:,]" '{print $2|"xargs"}' infile | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'

Corrected the filename and now try with the below command

$ nawk -F"[:,]" '{print $2|"xargs"}' fps.csv | nawk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print "Average : "s/NF}'
1 Like

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?

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 ----------

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

Halo itkamaraj

I try to run this code (ami.pl)

$ 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.

In windows

start -> run -> cmd -> goto the fps.csv folder

type the entire line as given below and press enter

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

And let me know the results

NOTE : make sure you defined the perl path in PATH variable

perl -F: -anE '$total += $F[1]; END { say $total/$. }' INPUTFILE

I get the answer by using cmd command. My perl program is at C:\Perl64\bin

Is this is the correct way to defined perl path on windows?

#C:\Perl64\bin\perl

or

#C:/Perl64/bin/perl

In the command prompt, type PATH and check whether the bin\perl is already included

PATH=C:\Program Files (x86)\Intel\Media SDK\3.0.442.32245 Beta3\bin\win32;C:\Per
l64\site\bin;C:\Perl64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wb
em;C:\Windows\System32\WindowsPowerShell\v1.0\

In this folder I only include csv files.

 
PATH=C:\Program Files (x86)\Intel\Media SDK\3.0.442.32245 Beta3\bin\win32;C:\Per
l64\site\bin;C:\Perl64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wb
em;C:\Windows\System32\WindowsPowerShell\v1.0\

Path is already set in your windows machine

Can you try this

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

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.

---------- 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.

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

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

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?

dir fps.csv

tyler_durden

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

Well, it works for me (Windows XP):

C:\work>perl -F: -anE "$total += $F[1]; END { say $total/$. }" fps.csv
30.1827114285714