Average of elements throught multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average of elements throught multiple files
# 8  
Old 08-08-2008
Code:
 perl -MIO::File -MList::Util=sum -e '@_=map{new IO::File$_,"r"}@ARGV;for(;;){print sum(map{exit unless <$_>}@_)/@_."\n"}' *

Not mine...from a trustable guru sitting near me who was interested......

As for the "from a certain line" his answer was "just tail the output - which is fair :-)
# 9  
Old 08-08-2008
a nawk solution:

Code:
 nawk '{x[FNR]+=$0;y[FNR]++}END{while(y[++i]){printf("%.3f\n",x[i]/y[i])}}' *

again - tail the output ie | tail+380000
# 10  
Old 08-08-2008
Hello,

I got following solution:

Code:
#!/bin/bash

filecount=0
for file in data*.ip
do
sed -i -n '382976, $p' $file
sed -i 's/[.]/,/g' $file
filecount=$(( filecount + 1))
echo $filecount
done

paste data*.ip > data.ip

cat data.ip | awk '{ printf("%.12f\n", ($1+$2+$3+$4+$5+$6+$7+$8+$9+$10+$11+$12+$13+$14+$15+$16+$17+$18+$19+$20+$21+$22+$23+$24+$25+$26+$27+$28+$29+$30+$31+$32+$33+$34+$35+$36+$37+$38+$39+$40+$41+$42+$43+$44+$45+$46+$47+$48+$49+$50+$51+$52+$53+$54+$55+$56+$57+$58+$59+$60+$61+$62+$63+$64+$65+$66+$67+$68+$69+$70+$71+$72+$73+$74+$75+$76+$77+$78+$79+$80+$81+$82+$83+$84+$85+$86+$87+$88+$89+$90+$91+$92+$93+$94+$95+$96+$97+$98+$99+$100)/100) }' > data.out

sed -i 's/[,]/./g' data.out

Its not really elegant, but it is working so far.
How can I do an easier average?

This is not really good as well:

Code:
sed -i -n '382976, $p' $file
sed -i 's/[.]/,/g' $file

How can i combine both? Tried it with the -e parameter, didnt work...
And how can I write to multiple seperate files, without the -i?
(data01.txt to data01_new.txt for example)


Isnt it possible to use parameters in sed?
Example:

Code:
#!/bin/bash

filecount=0
for file in data*.ip
do
sed -i -n '${1}, $p' $file
sed -i 's/[.]/,/g' $file
filecount=$(( filecount + 1))
echo $filecount
done

Code:
#!/bin/bash

row=${1}
filecount=0
for file in data*.ip
do
sed -i -n 'row, $p' $file
sed -i 's/[.]/,/g' $file
filecount=$(( filecount + 1))
echo $filecount
done

How can I cut row with sed from an defined row downwards?
And how can I paste rows after the cutting position?
(File1 has 1'000'000 row, I just want keep the first 300'000. File2 has 700'000 rows, I want paste these in File1 starting at position 300'001)

Thank you very much and a very nice weekend...
chillmaster
# 11  
Old 08-08-2008
Hi Tytalus,

thanks for answer.
You posted while I was writing.

Looks really short, will check both asap and post a solution...

So long
chillmaster
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare multiple arrays elements using awk

I need your help to discover missing elements for each box. In theory each box should have 4 items: ITEM01, ITEM02, ITEM08, and ITEM10. Some boxes either have a missing item (BOX02 ITEM08) or might have da duplicate item (BOX03 ITEM02) and missing another one (BOX03 ITEM01). file01.txt ... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

Match first two columns and average third from multiple files

I have the following format of input from multiple files File 1 24.01 -81.01 1.0 24.02 -81.02 5.0 24.03 -81.03 0.0 File 2 24.01 -81.01 2.0 24.02 -81.02 -5.0 24.03 -81.03 10.0 I need to scan through the files and when the first 2 columns match I... (18 Replies)
Discussion started by: ncwxpanther
18 Replies

3. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

4. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

5. Shell Programming and Scripting

Average of a column in multiple files

I have several sequential files with name stat.1000, stat.1001....to stat.1020 with a format like this 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0... (6 Replies)
Discussion started by: kayak
6 Replies

6. Shell Programming and Scripting

Computing average and standard deviation from multiple text files

Hello there, I found an elegant solution to computing average values from multiple text files awk '{for (i=1;i<=NF;i++){if ($i!~"n/a"){a+=$i}else{b++}}}END{for (i=1;i<=FNR;i++){for (j=1;j<=NF;j++){printf (a/(3-b))((b>0)?"~"b" ":" ")};printf "\n"}}' file1 file2 file3 I tried to modify... (2 Replies)
Discussion started by: charmmilein
2 Replies

7. Shell Programming and Scripting

Array output through a for loop problematic with multiple elements.

This code works perfect when using a machine with only one interface online. (Excluding the loopback of course) But when I have other interface up for vmware or a vpn the output gets mixed up. I know I had this working when I was just reading ip's from files so I know it is not a problem with... (8 Replies)
Discussion started by: Azrael
8 Replies

8. Shell Programming and Scripting

Computing average values from multiple text files

Hi, first, I have searched in the forum for this, but I could not find the right answer. (There were some similar threads, but I was not sure how to adapt the ideas.) Anyway, I have a quite natural problem: Given are several text files. All files contain the same number of lines and the same... (3 Replies)
Discussion started by: rbredereck
3 Replies

9. UNIX for Dummies Questions & Answers

Average for repeated elements in a column

I have a file that looks like this 452 025_E3 8 025_E3 82 025_F5 135 025_F5 5 025_F5 23 025_G2 38 025_G2 71 025_G2 9 026_A12 81 026_A12 10 026_A12 some of the elements in column2 are repeated. I want an output file that will extract the... (1 Reply)
Discussion started by: FelipeAd
1 Replies

10. Shell Programming and Scripting

Help in extracting multiple files and taking average at same time

Hi, I have 20 files which have respective 50 lines with different values. I would like to process each line of the 50 lines in these 20 files one at a time and do an average of 3rd field ($3) of these 20 files. This will be output to an output file. Instead of using join to generate whole... (8 Replies)
Discussion started by: ahjiefreak
8 Replies
Login or Register to Ask a Question