Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hwo to use awk to collect information in mutiple records

Status
Not open for further replies.

will27

Technical User
Jun 13, 2007
23
0
0
US
Dear all:
I am new to awk and hope to know an efficient way to achieve following object:

suppose the dataset is as following:

a 40
b 10
c 10
c 20
b 20
c 30
a 30
c 20

what I want to do is
first to collect information on a
a 40
a 30

2nd, calculate a summary information, say mean, in this case it is 35. [(40+30)/2]=35

3rd, paste this information as the 3rd field in each row containing a, as following
a 40 35
a 30 35

finally the dataset looks like:
a 40 35
b 10 15
c 10 20
c 20 20
b 20 15
c 30 20
a 30 35
c 20 20

thank you in advance.
regards

will







 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You can try something like that:
Code:
#
# Proceed all records
#

{
   records[NR] = $0;   # Memorize record
   keys[NR]    = $1;   # Memorize record key
   ++counts[$1];       # Count records with this key
   sums[$1]   += $2;   # Summurize values for this key
}

#
# End of datas
#

END {
   #
   # Compute average value for eack key found
   #
   for (s in sums) {
      sums[s] /= counts[s];
   }
   #
   # Print records and average value for the record key
   #
   for (c=1; c<=NR; c++) {
      print records[c], sums[keys[c]];
   }
}

Jean-Pierre.
 
Jean

This helps a lot, especially sums[keys[c]].

thank you

will
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top