LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2009, 11:28 PM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
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!

Last edited by Mike_V; 05-15-2009 at 01:31 PM.
 
Old 05-15-2009, 12:06 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what have you tried?
 
Old 05-15-2009, 12:15 AM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
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.

Last edited by Mike_V; 05-15-2009 at 12:16 AM.
 
Old 05-15-2009, 12:30 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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?
 
Old 05-15-2009, 12:47 AM   #5
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
c'mon...
 
Old 05-15-2009, 02:00 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Give it a try. We'll help you debug, but we're not going to do it for you.
 
Old 05-15-2009, 02:12 AM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
Old 05-15-2009, 03:17 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 05-15-2009, 08:49 AM   #9
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
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!

Last edited by Mike_V; 05-18-2009 at 09:46 AM.
 
Old 05-15-2009, 11:04 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 05-15-2009, 11:26 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Mike_V View Post
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.
 
Old 05-15-2009, 12:23 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by Mike_V View Post
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 View Post
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.

Last edited by schneidz; 05-15-2009 at 12:26 PM.
 
Old 05-15-2009, 01:17 PM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by schneidz View Post
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.
 
Old 05-15-2009, 01:37 PM   #14
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by schneidz View Post
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 View Post
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.
 
Old 05-15-2009, 01:42 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Mike_V View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
Encoding separate audio channels to separate files omnio Linux - Software 0 06-01-2007 07:46 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
Looking for a tool to auto crop and separate images in to separate files.. mlsfit Linux - Software 2 08-06-2006 03:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration