×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

summing up column total and print it beside the last record..

summing up column total and print it beside the last record..

summing up column total and print it beside the last record..

(OP)
Hi

I have a file like
2291, 382718.00
2291, 19338.00
2291, 9073.00
2292, 4707.00
2293, 495847.00
2293, 157310.00
2293, 63582.00
2293, 100059.00
2293, 2843.00
2293, 58597.00
2293, 14836.00
2293, 24204.00

I want to sum the second column and place the result beside the last record
whenever the first column value changes.

2291, 382718.00 ,
2291, 19338.00 ,
2291, 9073.00 , 411129.00
2292, 4707.00 , 4707.00
2293, 495847.00 ,
2293, 157310.00 ,
2293, 63582.00 ,
2293, 100059.00 ,
2293, 2843.00 ,
2293, 58597.00 ,
2293, 14836.00 ,
2293, 24204.00 , 917278.00

My program is adding all the second column values and giving the result
at the end. I am not able to break the totals depending on the first column values.

awk '{
a[NR] = $0
sum += $2
}
END {
for (x = 1; x <= NR-1; x++) {
printf"%s\n", a[x]
}
printf"%s %s\n", a[NR],sum
}'

Please help









RE: summing up column total and print it beside the last record..

Hi

CODE --> Awk

{
  a[NR] = $0
  f[NR] = $1                             # will need the 1st field separately too
  sum[$1] += $2                          # build subtotals by 1st field, not grand total
}
END {
  for (x = 1; x <= NR; x++) {            # iterate over all records, not the last one is the special, but each group's last
    if (f[x] == f[x + 1]) {              # current 1st field equals with next 1st field means not at groups end yet
      printf "%s\n", a[x]
    } else {
      printf "%s %s\n", a[NR], sum[f[x]] # also output the current group's subtotal
    }
  }
} 

Feherke.
feherke.github.io

RE: summing up column total and print it beside the last record..

(OP)
Thank you sir.. nice usage of arrays. Could you please provide any link of arrays examples?

RE: summing up column total and print it beside the last record..

(OP)

RE: summing up column total and print it beside the last record..

Hi

Oops, now I see I had an error above :

CODE --> Awk

{
  a[NR] = $0
  f[NR] = $1                             # will need the 1st field separately too
  sum[$1] += $2                          # build subtotals by 1st field, not grand total
}
END {
  for (x = 1; x <= NR; x++) {            # iterate over all records, not the last one is the special, but each group's last
    if (f[x] == f[x + 1]) {              # current 1st field equals with next 1st field means not at groups end yet
      printf "%s\n", a[x]
    } else {
      printf "%s %s\n", a[x], sum[f[x]]  # also output the current group's subtotal
    }
  }
} 
Sorry, that sneaked in somehow after the last double check.

Anyway, generally is not really effective to load the entire file into memory just to calculate running sums, so would try this alternative too :

CODE --> Awk

{
    if (f != $1) {
        if (NR > 1)
            print a ", " sum
        sum = 0
    } else {
        print a
    }
    a = $0
    f = $1
    sum += $2
}
END {
    print a ", " sum
} 

Feherke.
feherke.github.io

RE: summing up column total and print it beside the last record..

(OP)
yes sir..i realised the a[x] after posting the querry. Thank you

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