Input:
2.58359023380340e+02
1.43758864405595e+02
-7.65700666212508e+00
1.06460208083228e+02
1.26185441783936e+02
-3.41389169427027e+01
-1.40393299309592e+02
-3.07758776849508e+01
1.45067703495838e+02
1.79405834959073e+02
5.06666234594205e+01
OUT
2.0105894389e+02 (average of record 1 +2)
1.1632282493e+02 (average of record 3+4)
etc.
I need a script that takes every 2 records and averages them out,
so a file with 50,000 records would be converted to a file of 25,000 records.
I tried with this script but it is not working:
(NR%2)!=0 {
for (i=1; i<=NF; i++){
sum+=$i
}
}
(NR%2)==0 {
for (i=1; i<=NF; i++) printf "%f ", sum/2
printf "\n"
delete sum
}
Ygor
2
Seems to work okay...
$ cat file1
2.58359023380340e+02
1.43758864405595e+02
-7.65700666212508e+00
1.06460208083228e+02
1.26185441783936e+02
-3.41389169427027e+01
-1.40393299309592e+02
-3.07758776849508e+01
1.45067703495838e+02
1.79405834959073e+02
5.06666234594205e+01
$ awk '
> (NR%2)!=0 {
> for (i=1; i<=NF; i++){
> sum+=$i
> }
> }
> (NR%2)==0 {
> for (i=1; i<=NF; i++) printf "%f ", sum/2
> printf "\n"
> delete sum
> }
> ' file1
129.179512
-3.828503
63.092721
-70.196650
72.533852
$
What did you get?
awk '{sum+=$1}!(NR%2){print sum/2;sum=0}END{if(NR%2) print sum}' OFMT='%.10e' file
giving
2.0105894389e+02
4.9401600711e+01
4.6023262421e+01
-8.5584588497e+01
1.6223676923e+02
5.0666623459e+01
for your sample.
1 Like
You forgot to add the numbers of the even lines. The code sould be :
sum/2 ==> (sum + $i)/2
1 Like
Thanks, elixir, this works it seems.
How would I change the script so if there is an odd number of values, the last record is discarded?
Just drop the END pattern and the corresponding action:
awk '{sum+=$1}!(NR%2){print sum/2;sum=0}' OFMT='%.10e' file
Ok, but this script removes the final record even for files with an even number of records.
pamu
8
for even records Elixir's solution works perfect..
For odd records - It will print last column.
Try with this..
awk '{sum+=$1}!(NR%2){print sum/2;sum=0}END{if(sum != 0) {print sum}}' OFMT='%.10e' file
msabhi
9
$ awk '{m=$1;getline;sum=(m+$1)/2;print sum;}' OFMT='%.10e' new
OUTPUT:
2.0105894389e+02
4.9401600711e+01
4.6023262421e+01
-8.5584588497e+01
1.6223676923e+02
5.0666623459e+01