Sponsored Content
Top Forums Shell Programming and Scripting awk based script to find the average of all the columns in a data file Post 302640557 by ks_reddy on Tuesday 15th of May 2012 03:56:31 AM
Old 05-15-2012
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-programmin...ge-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 the below input format.
Code:
H1,H2,H3,H4
1,2,3,4
1,3,4,5
2,4,5,6
3,4,5,6
5,6,7,8
5,5,4,5

I got the below code which gives the average of all rows in every column and printing the output like below.
Avg.of.col1,avg.of.col2,avg.of.col3,avg.of.col4

Code:
FNR==1 { nf=NF} {  for(i=1; i<=NF; i++)    arr[i]+=$i   fnr=FNR } END {   for( i=1; i<=nf; i++)    printf("%.2f%s", arr[i] / fnr, (i==nf) ? "\n" : FS) }

But I need the below kind of code (complete shell script)
Code:
awk -F,  '$1==specificvalue{Code for average of all columns and print one by one}' inputfile.

The variable specificvalue mentioned in the script is automatically generated by a for loop.

Regards
Sidda
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

AWK script to split data and find average

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... (8 Replies)
Discussion started by: chrisjorg
8 Replies

3. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

4. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

5. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

6. Emergency UNIX and Linux Support

Average columns based on header name

Hi Friends, I have files with columns like this. This sample input below is partial. Please check below for main file link. Each file will have only two rows. ... (8 Replies)
Discussion started by: jacobs.smith
8 Replies

7. Shell Programming and Scripting

Average across multiple columns - awk

Hi forum members, I'm trying to get an average of multiple columns in a csv file using awk. A small example of my input data is as follows: cu,u3o8,au,ag -9,20,-9,3.6 0.005,30,-9,-9 0.005,50,10,3.44 0.021,-9,8,3.35 The following code seems to do most of what I want gawk -F","... (6 Replies)
Discussion started by: theflamingmoe
6 Replies

8. Shell Programming and Scripting

Find columns in a file based on header and print to new file

Hello, I have to fish out some specific columns from a file based on the header value. I have the list of columns I need in a different file. I thought I could read in the list of headers I need, # file with header names of required columns in required order headers_file=$2 # read contents... (11 Replies)
Discussion started by: LMHmedchem
11 Replies

9. 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

10. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies
td_ta_enable_stats(3C_DB)				Threads Debugging Library Functions				 td_ta_enable_stats(3C_DB)

NAME
td_ta_enable_stats, td_ta_reset_stats, td_ta_get_stats - collect target process statistics for libc_db SYNOPSIS
cc [ flag... ] file... -lc_db [ library... ] #include <proc_service.h> #include <thread_db.h> td_err_e td_ta_enable_stats(const td_thragent_t *ta_p, int on_off); td_err_e_stats td_ta_reset(const td_thragent_t *ta_p); td_err_e td_ta_get_stats(const td_thragent_t *ta_p, td_ta_stats_t *tstats); DESCRIPTION
The controlling process can request the collection of certain statistics about a target process. Statistics gathering is disabled by default. Each target process has a td_ta_stats_t structure that contains current values when statistic gathering is enabled. The td_ta_enable_stats() function turns statistics gathering on or off for the process identified by ta_p, depending on whether or not on_off is non-zero. When statistics gathering is turned on, all statistics are implicitly reset as though td_ta_reset_stats() had been called. Statistics are not reset when statistics gathering is turned off. Except for nthreads and r_concurrency, the values do not change further, but they remain available for inspection by way of td_ta_get_stats(). The td_ta_reset_stats() function resets all counters in the td_ta_stats_t structure to zero for the target process. The td_ta_get_stats() function returns the structure for the process in tstats. The td_ta_stats_t structure is defined in <thread_db.h> and contains the following members: typedef struct { int nthreads; /* total number of threads in use */ int r_concurrency; /* requested concurrency level */ int nrunnable_num; /* numerator of avg runnable threads */ int nrunnable_den; /* denominator of avg runnable threads */ int a_concurrency_num; /* numerator, avg achieved concurrency */ int a_concurrency_den; /* denominator, avg achieved concurrency */ int nlwps_num; /* numerator, avg number of LWPs in use */ int nlwps_den; /* denominator, avg number of LWPs in use */ int nidle_num; /* numerator, avg number of idling LWPs */ int nidle_den; /* denominator, avg number of idling LWPs */ } td_ta_stats_t; The nthreads member is the number of threads that are currently part of the target process. The r_concurrency member is the current requested concurrency level, such as would be returned by thr_setconcurrency(3C). The remaining members are averages over time, each expressed as a fraction with an integral numerator and denominator. The nrunnable_num and nrunnable_den members represent the average num- ber of runnable threads. The a_concurrency_num and a_concurrency_den members represent the average achieved concurrency, the number of actually running threads. The a_concurrency_num and a_concurrency_den members are less than or equal to nrunnable_num and nrunnable_den, respectively. The nlwps_num and nlwps_den members represent the average number of lightweight processes (LWPs) participating in this process. They must be greater than or equal to a_concurrency_num and a_concurrency_den, respectively, since every running thread is assigned to an LWP, but there can at times be additional idling LWPs with no thread assigned to them. The nidle_num and nidle_den members represent the average number of idle LWPs. RETURN VALUES
TD_OK The call completed successfully. TD_BADTA An invalid internal process handle was passed in. TD_DBERR A call to one of the imported interface routines failed. TD_ERR Something else went wrong. ATTRIBUTES
See attributes(5) for description of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT Level |Safe | +-----------------------------+-----------------------------+ SEE ALSO
libc_db(3LIB), thr_getconcurrency(3C), attributes(5) SunOS 5.11 20 Oct 1998 td_ta_enable_stats(3C_DB)
All times are GMT -4. The time now is 03:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy