two line averaging
hi guys,
i have output data from one code that needs to get read into another, but the second code can't accept as high of data rate, so i need to average between every two lines. there are seven columns in the file and can be 100 or more rows, but the EOF will be an even number, and the second column can be ignored. it's a constant. here's an output to input example: the output of the first code would look like line1 42 a b c d e line2 42 aa bb cc dd ee line3 42 aaa bbb ccc ddd eee line4 42 aaaa bbbb cccc dddd eeee . . . so with the above example the input file to the next code would look like line1 42 ave(a,aa) ave(b,bb) ave(c,cc) ave(d,dd) ave(e,ee) line2 42 ave(aaa,aaaa) ave(bbb,bbbb) ave(ccc,cccc) ave(ddd,dddd) ave(eee,eeee) . . . tabby |
I have an idea of how I would do it
what have you tried? what OS ? ( I see a Mac badge ) |
good morning Firerat (of the Princess Bride Swamp Firerats? i love that movie!)
i couldn't sleep laying in bed last night so i typed it from my mac laptop. my desktop is RHEL 5.5 Tikanga so far i have: Code:
#!/usr/bin/awk -f and Code:
#!/usr/bin/awk -f thanks for your help!!! tabby |
do you have a better example of the inputs?
can you give 10 lines, and also show the result you expect for instance, do you want the average of all the rows for each column input Code:
Line1 1 Code:
Line1 1 Code:
Line1 1 |
the original poster made a good faith effort so even if this is homework i think this guidence wont be cheating (although i have no idea where the variable n above comes from -- i get division by 0 errors when i try to run it as is because it is never defined).
heres my stab at it... (i did first 2 feilds only because i got bored -- season to taste): Code:
[schneidz@hyper ~]$ cat tabbygirl1990.txt |
here's the innie :)
Code:
1 42 0.19796486 0.362090835 0.354344909 0.856582877 0.735671789 Code:
1 42 ave(col3,row1&row2) ave(col4,row1&row2) ave(col5,row1&row2) ave(col6,row1&row2) ave(col7,row1&row2) tabby |
Maybe you need something like this:
Code:
#!/usr/bin/awk -f |
hi schneidz,
i tried your awk command and i got syntax errors, and i don't understand what's Code:
-- Code:
awk 'NR % 2 == 0 {sum1+=$1;sum2+=$2} NR %2 == 1 {sum1=$1;sum2=$2} NR % 2 == 0 {print "average-1 = " sum1/2} {print "average-2 = " sum2/2}' tabbygirl1990.dat Code:
average-2 = 21 |
can you please copy-paste the command and the error you are getting ?
|
./Script.sh /path/to/tabbygirl1990.dat
Code:
#!/bin/bash Code:
1 42 0.19796486 0.362090835 0.354344909 0.856582877 0.735671789 Code:
Line1 42 0.11149090550 0.24450236250 0.28229041700 0.63717809900 0.41367884550 0.19796486 and 0.025016951 are very different, thus leading to mean of 0.11149090550 |
colucix does the trick
can you explain what the _[i] is doing? i know the "i" is an iterator but the rest of it i haven't seen before also there are two printf and one print statement, i understand what the last print is doing but what are the printf statments doing in this script, i mean i know what a printf is just not what they are doing in the script thanks guys!!! tabby |
1. The
Code:
_[i] 2. The first printf statement prints out the new line number using a C notation to increment the variable c by one, before it's valued is used Code:
++c 3. The second printf statement prints out the average of the i-th field, as per your requirement. It is the body of the second for loop, which is executed from the 3rd field to the last one. |
thanks!
i thought that the _[] was some kind of special character/operator on i i know that ++ is standard C notation for iterate over (although i'm not sure of the diff between ++i or i++ i'll try to find out), but i hadn't seen the little c before so I didn't know that it is a variable so are the printf statements, kinds like storing/holding the data in the for loops thanks soooo much, tabby |
back again guys,
now i'd like to create averaged lines of data from their nearest neighbors, so like here's an input file Code:
1 42 0.0 0.4 0.3 0.8 0.7 Code:
1 42 0.0 0.4 0.3 0.8 0.7 tabby |
maybe a better example input file cause it has more lines
Code:
1 42 0.0 0.4 0.3 0.8 0.7 Code:
1 42 0.0 0.4 0.3 0.8 0.7 |
All times are GMT -5. The time now is 11:50 PM. |