calculate the average of cells in columns in separate txt files
Hi there,
I have file1.txt that contains: Code:
5.5 Code:
10.5 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 Any help is appreciated a lot! |
what have you tried?
|
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. |
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?
|
c'mon...
|
Give it a try. We'll help you debug, but we're not going to do it for you.
|
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.
|
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.
|
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 http://www.gnu.org/software/gawk/manual/gawk.html and some little adjustments. Cheers! |
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. |
Quote:
|
Quote:
___________________ Quote:
|
Quote:
Also, good job on finding a solution yourself, it isn't all that hard. |
Quote:
Quote:
|
Quote:
|
All times are GMT -5. The time now is 09:36 AM. |