AWK script to split data and find average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK script to split data and find average
# 1  
Old 09-14-2012
AWK script to split data and find average

Input:

Code:
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
Code:
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:

Code:
(NR%2)!=0 {
        for (i=1; i<=NF; i++){
                sum[i]+=$i
        }
}
(NR%2)==0 {
        for (i=1; i<=NF; i++) printf "%f ", sum[i]/2
        printf "\n"
        delete sum
}

# 2  
Old 09-14-2012
Seems to work okay...
Code:
$ 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]+=$i
>         }
> }
> (NR%2)==0 {
>         for (i=1; i<=NF; i++) printf "%f ", sum[i]/2
>         printf "\n"
>         delete sum
> }
> ' file1
129.179512
-3.828503
63.092721
-70.196650
72.533852

$

What did you get?
# 3  
Old 09-14-2012
Code:
awk '{sum+=$1}!(NR%2){print sum/2;sum=0}END{if(NR%2) print sum}' OFMT='%.10e' file

giving
Code:
2.0105894389e+02
4.9401600711e+01
4.6023262421e+01
-8.5584588497e+01
1.6223676923e+02
5.0666623459e+01

for your sample.
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 09-14-2012
Quote:
Originally Posted by chrisjorg
I tried with this script but it is not working:

Code:
(NR%2)!=0 {
        for (i=1; i<=NF; i++){
                sum[i]+=$i
        }
}
(NR%2)==0 {
        for (i=1; i<=NF; i++) printf "%f ", sum[i]/2
        printf "\n"
        delete sum
}

You forgot to add the numbers of the even lines. The code sould be :
Code:
sum[i]/2 ==> (sum[i] + $i)/2

This User Gave Thanks to delugeag For This Post:
# 5  
Old 09-14-2012
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?

Last edited by chrisjorg; 09-14-2012 at 05:03 PM..
# 6  
Old 09-15-2012
Quote:
Originally Posted by chrisjorg
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:
Code:
awk '{sum+=$1}!(NR%2){print sum/2;sum=0}' OFMT='%.10e' file

# 7  
Old 09-15-2012
Ok, but this script removes the final record even for files with an even number of records.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

awk - split data based on the count

Greetings Experts, I am generating a validation query through awk and facing an issue, which I need to overcome by splitting the data based on the pattern matching count in the value of an array. File1 -- Table11@column1@date@Table21@column1@varchar(10)@d;... (4 Replies)
Discussion started by: chill3chee
4 Replies

3. Shell Programming and Scripting

awk to split and parse unpredictable data

data.txt: CRITICAL: iLash: 97.00%, SqlPlus: 99.00%. Warning/critical thresholds: 95/98% I need to pull only the disknames: iLash and SqlPlus The following command will only pull iLash: echo "CRITICAL: iLash: 97.00%, SqlPlus: 99.00%. Warning/critical thresholds: 95/98%" | awk -F":"... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Awk based script to find the median of all individual columns in a data file

Hi All, I have some data like below. Step1,Param1,Param2,Param3 1,2,3,4 2,3,4,5 2,4,5,6 3,0,1,2 3,0,0,0 3,2,1,3 ........ so on Where I need to find the median(arithmetic) of each column from Param1...to..Param3 for each set of Step1 values. (Sort each specific column, if the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. Shell Programming and Scripting

Split File data using awk

HI Guys, I need to split the file in to number of files . file contains FILEHEADER and EOF . I have to split n number of times . I have to form the file with each splitted message between FILEHEADER and EOF using awk beign and end . how to implement please suggest. (2 Replies)
Discussion started by: manish8484
2 Replies

7. Shell Programming and Scripting

awk - find average interarrival times for each unique page

All, I have a test file as specified below. 1st col is <arrival time> and 2nd col is <Page #>. I want to find the inter-arrival time of requests for each page # (I've done this part already). Once I have this, I want to calculate the average interarrival time. Note, that I am trying to have the... (11 Replies)
Discussion started by: jontjioe
11 Replies

8. Shell Programming and Scripting

awk script to split field data

Hi Experts, I have a Input.txt document which contains data fields seperated by tabs. There are 4 fields totally Named UNIQUE, ORDER, CONTACT and WINS. The UNIQUE field contains unique ID and the CONTACT field contains data seperated by comma in some records. I am looking to write an awk script... (12 Replies)
Discussion started by: forumthreads
12 Replies

9. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

10. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies
Login or Register to Ask a Question