LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   calculate the average of cells in columns in separate txt files (https://www.linuxquestions.org/questions/programming-9/calculate-the-average-of-cells-in-columns-in-separate-txt-files-726127/)

Mike_V 05-14-2009 11:28 PM

calculate the average of cells in columns in separate txt files
 
Hi there,

I have file1.txt that contains:
Code:

5.5
4
3
5.5
5.5

and file2.txt that contains:
Code:

10.5
6
7.5
10.5
10.5

Both files have the same number of rows, only one column.

Now I'm looking for a script that can write me file_avg.txt that contains the average of the cells in the columns above:
Code:

8
10
5.25
8
8

One more thing: I have 5 files each with on column as above and I'd like to get an average (and if this works maybe more files)

Any help is appreciated a lot!

ghostdog74 05-15-2009 12:06 AM

what have you tried?

Mike_V 05-15-2009 12:15 AM

I've tried searching the forum and checked the "similar threads" below... but no luck.
Other than that nothing. I'm hoping there is a relatively easy solution, and that one of you have that "easy" solution... I know it can be done with Matlab but I'd have to learn basic Matlab first... which may take a lot of time.

ghostdog74 05-15-2009 12:30 AM

you have been exposed to shell scripting in your previous posts. i don't believe you can't at least produce something now. remember awk? you can use it to do calculation like this. also, the bc tool. programming languages like Python, Perl too can be used. What have you learnt so far since 20++ posts ago?

Mike_V 05-15-2009 12:47 AM

c'mon...

chrism01 05-15-2009 02:00 AM

Give it a try. We'll help you debug, but we're not going to do it for you.

jiml8 05-15-2009 02:12 AM

It certainly is an easy enough problem. I'd probably do it in C just because I could do it fastest that way. When I say that I must also note that I have "in the can" routines to search the directory for the relevant files, build a list (including their sizes), then open them one by one. But it is also easy enough in bash, and trivial in PHP, Perl, or Python.

H_TeXMeX_H 05-15-2009 03:17 AM

I suggest using 'paste' to paste the files together, then it should be easy to use 'awk' to find the averages on a per-line basis.

Mike_V 05-15-2009 08:49 AM

OK, OK: after a night of sleep:

Code:

paste file1.txt file2.txt | awk '{ sum = $1 + $2 ; avg = sum / 2 ; print avg }' > file_avg.txt
and it didn't take much sleep, as you can imagine... just the gawk manual
http://www.gnu.org/software/gawk/manual/gawk.html
and some little adjustments.
Cheers!

theNbomr 05-15-2009 11:04 AM

Nicely done Mike_V. Not only did you accomplish your objective, but by my estimation, you pretty much nailed the definitive solution. Sometimes a little RTFMing goes a long way.
--- rod.

Sergei Steshenko 05-15-2009 11:26 AM

Quote:

Originally Posted by Mike_V (Post 3541798)
OK, OK: after a night of sleep:

paste file1.txt file2.txt | awk '{ sum = $1 + $2 ; avg = sum / 2 ; print avg }' > file_avg.txt

and it didn't take much sleep, as you can imagine... just the gawk manual
http://www.gnu.org/software/gawk/manual/gawk.html
and some little adjustments.
Cheers!

Once in Oregon I saw a license plate surrounded by plastic frame with "True men read manuals" on it.

schneidz 05-15-2009 12:23 PM

Quote:

Originally Posted by Mike_V (Post 3541369)
Hi there,

I have file1.txt that contains:
Code:

5.5
4
3
5.5
5.5

and file2.txt that contains:
Code:

10.5
6
7.5
10.5
10.5

Both files have the same number of rows, only one column.

Now I'm looking for a script that can write me file_avg.txt that contains the average of the cells in the columns above:
Code:

8
10
5.25
8
8

One more thing: I have 5 files each with on column as above and I'd like to get an average (and if this works maybe more files)

Any help is appreciated a lot!

i dont see how the average of 4 and 6 is 10. do you mean you want the sum of the two feilds ?
___________________

Quote:

Originally Posted by Mike_V (Post 3541798)
OK, OK: after a night of sleep:

paste file1.txt file2.txt | awk '{ sum = $1 + $2 ; avg = sum / 2 ; print avg }' > file_avg.txt

and it didn't take much sleep, as you can imagine... just the gawk manual
http://www.gnu.org/software/gawk/manual/gawk.html
and some little adjustments.
Cheers!

also, + 1 for at least making an attempt.

H_TeXMeX_H 05-15-2009 01:17 PM

Quote:

Originally Posted by schneidz (Post 3542033)
i dont see how the average of 4 and 6 is 10. do you mean you want the sum of the two feilds ?
___________________

also, + 1 for at least making an attempt.

that was probably a mistake, the rest make sense

Also, good job on finding a solution yourself, it isn't all that hard.

Mike_V 05-15-2009 01:37 PM

Quote:

Originally Posted by schneidz (Post 3542033)
i dont see how the average of 4 and 6 is 10. do you mean you want the sum of the two feilds ?

also, + 1 for at least making an attempt.

yeah... (6+4)/2=5, sorry.

Quote:

Originally Posted by Sergei Steshenko (Post 3541977)
Once in Oregon I saw a license plate surrounded by plastic frame with "True men read manuals" on it.

If all manuals were as clear as that gawk one, I would agree.

Sergei Steshenko 05-15-2009 01:42 PM

Quote:

Originally Posted by Mike_V (Post 3542097)
yeah... (6+4)/2=5, sorry.



If all manuals were as clear as that gawk one, I would agree.

If all people first at least tried to read manuals and then asked questions on things that were not clear to them, I would agree with you.


All times are GMT -5. The time now is 09:36 AM.