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!

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

Status
Not open for further replies.

visitnag

Programmer
Sep 21, 2006
15
0
0
IN
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









 
Hi

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


Feherke.
feherke.github.io
 
Thank you sir.. nice usage of arrays. Could you please provide any link of arrays examples?
 
Hi

Oops, now I see I had an [highlight]error[/highlight] above :
Code:
[teal]{[/teal]
  a[teal][[/teal]NR[teal]] =[/teal] [navy]$0[/navy]
  f[teal][[/teal]NR[teal]] =[/teal] [navy]$1[/navy]                             [gray]# will need the 1st field separately too[/gray]
  sum[teal][[/teal][navy]$1[/navy][teal]] +=[/teal] [navy]$2[/navy]                          [gray]# build subtotals by 1st field, not grand total[/gray]
[teal]}[/teal]
[b]END[/b] [teal]{[/teal]
  [b]for[/b] [teal]([/teal]x [teal]=[/teal] [purple]1[/purple][teal];[/teal] x [teal]<=[/teal] NR[teal];[/teal] x[teal]++) {[/teal]            [gray]# iterate over all records, not the last one is the special, but each group's last[/gray]
    [b]if[/b] [teal]([/teal]f[teal][[/teal]x[teal]] ==[/teal] f[teal][[/teal]x [teal]+[/teal] [purple]1[/purple][teal]]) {[/teal]              [gray]# current 1st field equals with next 1st field means not at groups end yet[/gray]
      [b]printf[/b] [i][green]"%s[/green][/i][lime]\n[/lime][i][green]"[/green][/i][teal],[/teal] a[teal][[/teal]x[teal]][/teal]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
      [b]printf[/b] [i][green]"%s %s[/green][/i][lime]\n[/lime][i][green]"[/green][/i][teal],[/teal] a[teal][[/teal][highlight]x[/highlight][teal]],[/teal] sum[teal][[/teal]f[teal][[/teal]x[teal]]][/teal]  [gray]# also output the current group's subtotal[/gray]
    [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal]
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:
[teal]{[/teal]
    [b]if[/b] [teal]([/teal]f [teal]!=[/teal] [navy]$1[/navy][teal]) {[/teal]
        [b]if[/b] [teal]([/teal]NR [teal]>[/teal] [purple]1[/purple][teal])[/teal]
            [b]print[/b] a [i][green]", "[/green][/i] sum
        sum [teal]=[/teal] [purple]0[/purple]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
        [b]print[/b] a
    [teal]}[/teal]
    a [teal]=[/teal] [navy]$0[/navy]
    f [teal]=[/teal] [navy]$1[/navy]
    sum [teal]+=[/teal] [navy]$2[/navy]
[teal]}[/teal]
[b]END[/b] [teal]{[/teal]
    [b]print[/b] a [i][green]", "[/green][/i] sum
[teal]}[/teal]


Feherke.
feherke.github.io
 
yes sir..i realised the a[x] after posting the querry. Thank you

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top