Average each numeric column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average each numeric column
# 8  
Old 02-13-2019
Code:
awk '
NR==1 { for (i=1; i<=NF; i++) if ($(i) ~ /^POP/) { printf "%-10s", "AVG_" $i; col[i]=i; }; print ""; next; }
{for (i=1; i<=NF; i++) if (col[i]) sum_col[i]+=$i;}
END { for (i=1; i<=NF; i++) if (col[i]) printf "%-10.2f", sum_col[i]/(NR-1); print ""; }
' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Advanced & Expert Users

Sort by second column numeric values

From googling and reading man pages I figured out this sorts the first column by numeric values. sort -g -k 1,1 Why does the -n option not work? The man pages were a bit confusing. And what if I want to sort the second column numerically? I haven't been able to figure that out. The file... (7 Replies)
Discussion started by: cokedude
7 Replies

3. Shell Programming and Scripting

Check first column - average second column based on a condition

Hi, My input file Gene1 1 Gene1 2 Gene1 3 Gene1 0 Gene2 0 Gene2 0 Gene2 4 Gene2 8 Gene3 9 Gene3 9 Gene4 0 Condition: If the first column matches, then look in the second column. If there is a value of zero in the second column, then don't consider that record while averaging. ... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Get column average using ID

I have a file that looks like this: id window BV 1 1 0.5 1 2 0.2 1 3 0.1 2 1 0.5 2 2 0.1 2 3 0.2 3 1 0.4 3 2 0.6 3 3 0.8 Using awk, how would I get the average BV for window 1? Output like this: window avgBV 1 0.47 2 0.23 (10 Replies)
Discussion started by: jwbucha
10 Replies

5. Shell Programming and Scripting

Remove certain column with numeric value

I have file1.txt LBP298W2,300,-18,-115,-12,-105 LBP298W2,300,-18,-115,LBP298W3,300 LBP298W3,300,-18,-115,-12,-105---------- Post updated at 03:35 AM ---------- Previous update was at 03:34 AM ---------- i want to remove every line with non numeric value in column 5 expected result ... (4 Replies)
Discussion started by: radius
4 Replies

6. Shell Programming and Scripting

Calculate the average of a column based on the value of another column

Hi, I would like to calculate the average of column 'y' based on the value of column 'pos'. For example, here is file1 id pos y c 11 1 220 aa 11 4333 207 f 11 5333 112 ee 11 11116 305 e 11 11117 310 r 11 22228 781 gg 11 ... (2 Replies)
Discussion started by: jackken007
2 Replies

7. Shell Programming and Scripting

average of rows with same value in the first column

Dear All, I have this file tab delimited A 1 12 22 B 3 34 33 C 55 9 32 A 12 81 71 D 11 1 66 E 455 4 2 B 89 4 3 I would like to make the average every column where the first column is the same, for example, A 6,5 46,5 46,5 B 46,0 19,0 18,0 C 55,0 9,0 32,0 D 11,0 1,0 66,0... (8 Replies)
Discussion started by: paolo.kunder
8 Replies

8. Shell Programming and Scripting

How to check if a column is having a numeric value or not in a file?

Hi, I want to know, how we find out if a column is having a numeric value or not. For Example if we have a csv file as ASDF,QWER,GHJK,123,FGHY,9876 GHTY,NVHR,WOPI,623,HFBS,5386 we need to find out if the 4th and 6th column has muneric value or not. Thanks in advance Keerthan (9 Replies)
Discussion started by: keerthan
9 Replies

9. UNIX for Dummies Questions & Answers

average of a column in a table

Hello, Is there a quick way to compute the average of a column data in a numerical tab delimeted file? Thanks, Gussi (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

10. UNIX for Dummies Questions & Answers

calculate average of column 2

Hi I have fakebook.csv as following: F1(current date) F2(popularity) F3(name of book) F4(release date of book) 2006-06-21,6860,"Harry Potter",2006-12-31 2006-06-22,,"Harry Potter",2006-12-31 2006-06-23,7120,"Harry Potter",2006-12-31 2006-06-24,,"Harry Potter",2006-12-31... (0 Replies)
Discussion started by: onthetopo
0 Replies
Login or Register to Ask a Question
DB2_COLUMNS(3)								 1							    DB2_COLUMNS(3)

db2_columns - Returns a result set listing the columns and associated metadata for a table

SYNOPSIS
resource db2_columns (resource $connection, [string $qualifier], [string $schema], [string $table-name], [string $column-name]) DESCRIPTION
Returns a result set listing the columns and associated metadata for a table. PARAMETERS
o $connection - A valid connection to an IBM DB2, Cloudscape, or Apache Derby database. o $qualifier - A qualifier for DB2 databases running on OS/390 or z/OS servers. For other databases, pass NULL or an empty string. o $schema - The schema which contains the tables. To match all schemas, pass '%'. o $table-name - The name of the table or view. To match all tables in the database, pass NULL or an empty string. o $column-name - The name of the column. To match all columns in the table, pass NULL or an empty string. RETURN VALUES
Returns a statement resource with a result set containing rows describing the columns matching the specified parameters. The rows are com- posed of the following columns: +------------------+---------------------------------------------------+ | Column name | | | | | | | Description | | | | +------------------+---------------------------------------------------+ | TABLE_CAT | | | | | | | Name of the catalog. The value is NULL if this | | | table does not have catalogs. | | | | | TABLE_SCHEM | | | | | | | Name of the schema. | | | | | TABLE_NAME | | | | | | | Name of the table or view. | | | | | COLUMN_NAME | | | | | | | Name of the column. | | | | | DATA_TYPE | | | | | | | The SQL data type for the column represented as | | | an integer value. | | | | | TYPE_NAME | | | | | | | A string representing the data type for the col- | | | umn. | | | | | COLUMN_SIZE | | | | | | | An integer value representing the size of the | | | column. | | | | | BUFFER_LENGTH | | | | | | | Maximum number of bytes necessary to store data | | | from this column. | | | | | DECIMAL_DIGITS | | | | | | | The scale of the column, or NULL where scale is | | | not applicable. | | | | | NUM_PREC_RADIX | | | | | | | An integer value of either 10 (representing an | | | exact numeric data type), 2 (representing an | | | approximate numeric data type), or NULL (repre- | | | senting a data type for which radix is not appli- | | | cable). | | | | | NULLABLE | | | | | | | An integer value representing whether the column | | | is nullable or not. | | | | | REMARKS | | | | | | | Description of the column. | | | | | COLUMN_DEF | | | | | | | Default value for the column. | | | | | SQL_DATA_TYPE | | | | | | | An integer value representing the size of the | | | column. | | | | |SQL_DATETIME_SUB | | | | | | | Returns an integer value representing a datetime | | | subtype code, or NULL for SQL data types to which | | | this does not apply. | | | | |CHAR_OCTET_LENGTH | | | | | | | Maximum length in octets for a character data | | | type column, which matches COLUMN_SIZE for sin- | | | gle-byte character set data, or NULL for non- | | | character data types. | | | | |ORDINAL_POSITION | | | | | | | The 1-indexed position of the column in the ta- | | | ble. | | | | | IS_NULLABLE | | | | | | | A string value where 'YES' means that the column | | | is nullable and 'NO' means that the column is not | | | nullable. | | | | +------------------+---------------------------------------------------+ SEE ALSO
db2_column_privileges(3), db2_foreign_keys(3), db2_primary_keys(3), db2_procedure_columns(3), db2_procedures(3), db2_special_columns(3), db2_statistics(3), db2_table_privileges(3), db2_tables(3). PHP Documentation Group DB2_COLUMNS(3)