I would like to calculate average value for data in multiple files: (about 10 files). Lets say I have file1.dat, file2.dat, file3.dat and … file10.dat.
Each file contains two columns. The “first column is line number” and the “second column is the data“. In total I have 1800 lines in a file. The pattern of the data is shown below:
1.00 0.659
2.00 0.608
3.00 0.578
4.00 0.557
5.00 0.543
6.00 0.527
7.00 0.514
8.00 0.502
9.00 0.489
.
.
.
1800.00 0.480
I want to calculate average for data according to line numbers from each file. That means, I want to find average for the all first line data from file1, file2… and file10 and write in new file. Then average for the all second line data from file1, file2… and file10 and write in the same new file.
That new file supposed to have the format as below. For example;
1.00 0.112
2.00 0.324
3.00 0.887
.
.
Where the first column is the line number and the second column is the average values for data. I shall explain bit detail for clarity purpose. In the above example, 0.112 is the average for all data in first line from each file. And 0.324 is the average value for all data in second line from each file.
What I want is the code which can accomplish this calculation.
I have tried with perl for openning a file. But lost when trying to figure out to convert the idea into code. The perl that I wrote is as below
#!/usr/bin/perl -w
open (FILE, "file1.dat") or die $!;
while (<FILE>) {
chomp;
print "$_\n";
}
close (FILE);
Thanks in advance.
I forgot almost everything about bash scripting. but I think you can do something like this.