Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Cumulative total for each device

Cumulative total for each device

Cumulative total for each device

I have iostat output in a file and need a cumulative total for each device (there may be a few or there could be tens of devices).
for example:


sdy 1.0
sdz 1.0
sdy 17.5
sdz 12.1
sdy 5.33
sdz 0.3 

This can be done like this in awk using:


awk '{await[$1]+=$10}END{for(j in await)print j" "await[j]}' x 

How can this be done using C to get output like:


sdy 19.18
sdz 14.19 

RE: Cumulative total for each device

What have you got so far?

RE: Cumulative total for each device

IMO, it's about using associative arrays in C.

RE: Cumulative total for each device

For this purpose std::map from C++ can be used:



// Compile Command:
// g++ data_proc.c -o data_proc

#include <iostream>
#include <fstream>
#include <iomanip>
#include <map>

using namespace std;

void process_data(void);

int main() {
  char dev_name[4];
  double dev_size;
  std::map<string, double> totals;
  std::map<string, double>::iterator totals_it,totals_end;
  FILE* file = fopen("data.txt","r"); 

  cout << "Reading Data :" << endl;
  while(fscanf(file, "%s %lf", dev_name, &dev_size) != EOF) {
    // print line
    cout << "dev_name = '" << dev_name << "', dev_size = " 
         << std::fixed << std::setw(5) << std::setprecision(2) 
         << dev_size << endl;
    // process data;

    if(totals[dev_name]) {
      totals[dev_name] += dev_size;
    else {
      totals[dev_name] += dev_size;
  // close file

  // print results
  cout << endl << "* Totals :" << endl;
  for( totals_it = totals.begin(), totals_end = totals.end();  
       totals_it != totals_end;  totals_it++ )  {
       cout <<  totals_it->first << " : " << totals_it->second << endl;
  // at end return



$ g++ data_proc.c -o data_proc

$ data_proc
Reading Data :
dev_name = 'sdy', dev_size =  1.00
dev_name = 'sdz', dev_size =  1.00
dev_name = 'sdy', dev_size = 17.50
dev_name = 'sdz', dev_size = 12.10
dev_name = 'sdy', dev_size =  5.33
dev_name = 'sdz', dev_size =  0.30

* Totals :
sdy : 23.83
sdz : 13.40 

RE: Cumulative total for each device

In the code above, there is a little typo in the assigning value to the associative array:


else {
  totals[dev_name] += dev_size;
It should be


else {
  totals[dev_name] = dev_size;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close