Writing a script to take the average of two columns every 3 rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Writing a script to take the average of two columns every 3 rows
# 1  
Old 11-18-2013
Writing a script to take the average of two columns every 3 rows

I have a dataset with 120 columns. I would like to write a script, that takes the average of every two columns, starting from columns 2 and 3, and moving consecutively in frames of 3 columns, all the way until the last column.

The first column in the output file would be the averages of columns 2 and 3, the second column would be the averages of columns 5 and 6, the third column would be the average of 8 and 9.

Sample Input:
Code:
0 1 0.5 2 3 4 6 0 2
0 2 3 9 4 5 0.1 3 8

Output:
Code:
0.75 3.5 1
2.5 4.5 5.5

Thank you!
# 2  
Old 11-18-2013
Try:
Code:
awk '{for (i=2;i<=NF;i+=3) printf ($i+$(i+1))/2" ";printf "\n"}' file

This User Gave Thanks to bartus11 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with script to convert rows to columns

Hello I have a large database with the following structure: Headword=Gloss1;Gloss2;Gloss3 The Glosses are separated by a ; What I need is to reduce the multiple glosses on each row to columns Headword=Gloss1 Headword=Gloss2 Headword=Gloss3 I had written the following script in awk... (5 Replies)
Discussion started by: gimley
5 Replies

2. UNIX for Advanced & Expert Users

Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism

I got a requirement where I need to compare two files wrt to each columns and write the corresponding difference in another file along with some identification showing mismatched columns. Pointing out the mismatched columns is my main problem statement. For example we have files like: File 1 ... (8 Replies)
Discussion started by: piyush pankaj
8 Replies

3. Shell Programming and Scripting

Shell script to convert rows to columns

Hi I have a file having the values like below ---------------------------- .set A col1=”ABC” col2=34 col3=”DEF” col4=”LMN” col5=25 .set A .set B col1=55 col3=”XYZ” col4=”PQR” col5=66 .set B .set C col2=”NNN” (1 Reply)
Discussion started by: Suneel Mekala
1 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

Convert columns to rows in perl script

Hi All I want to have a Perl script which convert columns to rows. The Perl should should read the data from input file. Suppose the input file is 7215484 date to date 173.3 A 1.50 2.23 8.45 10.14 2.00 4.50 2.50 31.32 7216154 month to month (3 Replies)
Discussion started by: parthmittal2007
3 Replies

6. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

7. Linux

help with columns and rows - script

Hi everyone, how can I convert a file with 3375 rows and 6 columns to a file with 1350 rows and 15 columns by using a script? Is it possible with awk or something like that? Any help is much appreciated. Thanks. D. (5 Replies)
Discussion started by: DKalfileritos
5 Replies

8. Shell Programming and Scripting

Script - columns to rows

Hi Everyone, I have a file text.cvs, which is a file with columns from excel. The data are separeted with semicolon. The content of the file looks like A B C D E F 1 3 3 4 3 3 2 2 1 2 5 2 5 6 1 1 2 1 Now I wish to write an script which writes the colums A, C and F in... (4 Replies)
Discussion started by: Tob26
4 Replies

9. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows?

Hi Friends, In continuation to my earlier post https://www.unix.com/shell-programming-scripting/99166-script-find-average-given-column-also-specified-number-rows.html I am extending my problem as follows. Input: Column1 Column2 MAS 1 MAS 4 ... (2 Replies)
Discussion started by: ks_reddy
2 Replies

10. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows??

Hi friends I have 100 files in my directory. Each file look like this.. Temp1 Temp2 Temp3 MAS 1 2 3 MAS 4 5 6 MAS 7 8 9 Delhi 10 11 12 Delhi 13 14 15 Delhi 16 17 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies
Login or Register to Ask a Question