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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summarizing Data in Columns 1

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
This is second part of previous thread. I need help with summarizing the results of data collection
process. The data is as follows:

short 1 1 455 17 8 0
short 1 1 455 20 6 2
short 1 1 455 20 4 2
short 1 1 345 20 4 0
short 1 2 405 17 8 0
short 1 2 220 17 6 2
med 1 1 455 17 8 0
med 1 1 450 20 6 2
med 1 1 425 20 6 0
med 1 1 420 20 4 2
med 1 1 415 20 0 4
med 1 2 410 17 8 0
med 1 2 415 20 6 2
med 1 2 410 20 6 0
med 1 2 350 17 6 0
med 1 2 315 20 4 2
med 1 2 310 20 2 2

I am trying to compute the sum of values in
col 7 for short and med data set. I would also like to
count the number of times col 5 changes to "20" for
short and med data sets.

Desired format:
sh/med col_2 squirt solv
short 1 1 6
med 1 3 14

I have tried the following, but having
problems debugging:

{for (i=1; $1 = first; $2 = second);
solv = solv + $7;
squirt = $5;
i = i + 1;
if ( $1 = first && $2 = second)
squirt = squirt + 1;
else
{ print $1, $2, squirt, solv
}

thanks,
thunderkid
 
Code:
{ solv[$1] += $7
  if ( $5 == 20 && col5[$1] != 20 )
    squirt[$1]++
  col5[$1] = $5
}
END { fmt = "%-6s%7s%6s\n"
  printf fmt, "size", "squirt", "solv"
  printf fmt, "-----", "------", "----"
  show( "short" )
  show( "med" )
}
function show(s)
{ printf fmt, s, squirt[s], solv[s]
}
 
futurelet,
Your solution worked like a charm. How would your solution change as modified below? See desired results.

short 1 1 455 17 8 0
short 1 1 455 20 6 2
short 1 1 455 20 4 2
short 1 1 345 20 4 0
short 1 2 405 17 8 0
short 1 2 220 17 6 2
med 1 1 455 17 8 0
med 1 1 450 20 6 2
med 1 1 425 20 6 0
med 1 1 420 20 4 2
med 1 1 415 20 0 4
med 1 2 410 17 8 0
med 1 2 415 20 6 2
med 1 2 410 20 6 0
med 1 2 350 17 6 0
med 1 2 315 20 4 2
med 1 2 310 20 2 2
med 2 2 415 10 6 2 <--- new data added starting here
med 2 2 410 20 6 0 for col 2
med 2 2 350 17 6 0
med 2 2 315 20 4 2
med 2 2 310 20 2 2

desired results:
size col2 squirt solv
----- ---- ------ ----
short 1 1 6
med 1 3 14
med 2 2 6

Thanks again. You get a star!
thunderkid
 

Code:
NF==7 { solv[$1,$2] += $7
  if ( $5 == 20 && col5[$1,$2] != 20 )
    squirt[$1,$2]++
  col5[$1,$2] = $5
  if ( $1 != sizes[i] )
    sizes[++i] = $1
}
END { fmt = "%-6s%5s%7s%6s\n"
  printf fmt, "size", "col2", "squirt", "solv"
  printf fmt, "-----", "----", "------", "----"
  show()
}
function show(    s, i, j)
{ for (i=1; i in sizes; i++)
  { s = sizes[i]
    for (j=1; (s,j) in solv; j++)
      printf fmt, s, j, squirt[s,j], solv[s,j]
  }
}
 
futuerlet
Once again you came through with a gem! There is much to be learned from you and others. Thanks. Have a star!

thunderkid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top