Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:48:46+00:00 2026-06-18T05:48:46+00:00

I would like to calculate average value for data in multiple files: (about 10

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:48:47+00:00Added an answer on June 18, 2026 at 5:48 am

    I forgot almost everything about bash scripting. but I think you can do something like this.

    files=(file1 file2 file3 file4)
    for i in `seq  4`
    do
      j=$(($i-1))
      f[$j]=`cat ./temp/${files[$i]} | awk '{print $2}' `
    done
    
    for i in `seq 0 1799`
    do
      sum=0
      rowValue=0
      for j in `seq 0 3`
      do
        fileContent=(${f[$j]})
        rowValue=`echo ${fileContent[$i]} `
        sum=$(($sum + $rowValue))    
      done
      echo "sum is" $i $sum
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I would like to calculate percents and do: double n = ...;
I have a series of datetime objects and would like to calculate the average
I have a matrix mat and would like to calculate the mean of the
Good morning, I have mysql queries where I would like to calculate percentage of
I'm trying to write this query, that would calculate the average value of all
I would like to use data.table to calculate a summary statistic, and then based
I have a data set that I need to calculate the trait average of
I would like to calculate total order amount in the previous month. I got
I would like to calculate my total order amount in the previous week. I
I would like to calculate the similarity between users, which is reciprocal. similarity[:user1][:user2] ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.