The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding Multiple Lines to Multiple Files dayinthelife Shell Programming and Scripting 2 06-04-2008 12:50 PM
calculate average of multiple line data smacherla HP-UX 4 05-23-2008 05:39 PM
script to find the average number or files? bbbngowc Shell Programming and Scripting 2 03-27-2008 12:57 PM
grep throught Zip file. deepakwins UNIX for Dummies Questions & Answers 1 01-30-2008 06:34 PM
find common elements in 2 files (for loop) ibking Shell Programming and Scripting 4 12-11-2007 04:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2008
chillmaster chillmaster is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
Average of elements throught multiple files

Hi,

I got a lot of files looking like this:
Code:
1
0.5
6
All together there are ard 1'000'000 lines in each of the ard 100 files.
I want to build the average for every line, and write the result to a new file.
The averaging should start at a specific line, here for example at line 380'000.

Help is much appreciated...

chillmaster

PS: This should be done for data files of CFD calculations, exactly to average a transient calculation and read it with postprocessor again.
So the first (here ard 380'000) lines are the coordinates of the field points following hereafter...
  #2 (permalink)  
Old 08-07-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,770
Do you mean average accross hundreds of files - ie., line 474000 average from files 1...200? This would mean each file has the same number of lines, which based on your explanation does not seem to be the case.
  #3 (permalink)  
Old 08-07-2008
chillmaster chillmaster is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
Hi jim,

yep, you are rite.
Maybe i wrote a little confused, English isnt my mother tongue...

Anyway, rite now I got 100 files each with 1'050'000 rows, just one column.
Every file is a specific point of time.
I want an average for each line over all the 100 files (Line 1 for all 100 files, Line 2 ...).
However, I need to start averaging at a specific line, something ard 380'000.
These first 380'000 rows are coordinate points.

I think first I need to join the files, that I get one file with 1'050'000 rows and 100 columns. I tried:
Code:
 cut -f 1 data0050_1.ip data0055_1.ip | pr -2 -t > test.ip
just for two files, but it isnt working.
It seems that there is a limit for the lenght of the files...

pressure 2
velocity 23
. 345
. .
. .
2 .
3 pressure
6 .
5 .
64 .
64
3
6

Pressure is supposed to be the first row...


Thank you for your help
  #4 (permalink)  
Old 08-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question Just thinking "out loud" on this

Could you (not in proper syntax)

while read filelisting
do
cat -n file >>bigfile
done

sort bigfile >sortedfile

you would then have sortedfile looking like
1 123.1222
.. ... (98 more)
1 1.509 (last)
2 56.789

Perhaps then awk could sum up all for field1 is the same

Two concerns:
(a) bigfile will be a REALLY long file
(c) will the next sort command die with the REALLY long file?
  #5 (permalink)  
Old 08-07-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,770
Great. I got it. Try something like this.
Assume your files have the same base name with numbers after them 1 - 100 or so and the files are called filename1... filename<nnn>
Code:
#!/bin/ksh

> outputfile
filecount=0
for file in filename*
do
	sed -n '380000, $p' $file >> outputfile
	filecount=$(( filecount + 1))
done
awk -v filecount=$filecount '
            BEGIN { i=1}
            { avg[ i % 670000]+=$1 }
     	END{for (i=1; i<= 670000; i++) { printf("%.3f\n", avg[i]/filecount) }) ' outputfile > avgfile
The %.3f format specifier controls the precision of the output, this rounds to 1000ths
  #6 (permalink)  
Old 08-08-2008
chillmaster chillmaster is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
Hello jim,

thank you very much for your help.
The first part is working, the second part brings a syntax error at awk line 3.
Unfortunately, I dont know anything about awk.
I will check the syntax in the next few days, since it seems to me very powerful.
I would be very happy, if you could help me again anyway, because I think I need some time to get the awk usage...

One more question about sorting the files:
The first part makes a really big file, while I cant see at which position one file ends and the next is starting. Actually, you set an awk comand with the number 670'000, I think thats the file lenght of each seperate file in the big outpufile, is that rite?
Anyway, do you know, how i can join multiple one column files to one multiple column file. Seems to me not to be a really difficult task, however, I cant find any helpful information.

Thank you very much
chillmaster
  #7 (permalink)  
Old 08-08-2008
chillmaster chillmaster is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
Addition:

I found the syntax error, the last bracket was the wrong one... ) > }
Anyway, it is not working rite, avgfile:
Code:
70124752,000
0,000
0,000
0,000
0,000
0,000
0,000
0,000
Tried with just 5 files.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0