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

Get the total of a field in all the lines

Status
Not open for further replies.

appsguy606

Programmer
Nov 5, 2008
2
US
Hi
I have a fixed format data file. In this data file I want the total of all the fields at position:21 of all the lines starting with 6. There lines starting with 6 for each group of line start wtih 5. In this case I should get 2 totals as there are 2 groups of 5 lines.


$ cat file
101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 000013768460000526 AMERICAN
62201100123409-1960 000004188960000526 AMERICAN
62201100123409-1960 000025960260000526 AMERICAN
62201100123409-1960 000009390060000526 AMERICAN
62201100123409-1960 000028661060000526 AMERICAN
62201100123409-1960 000004047360000526 AMERICAN
62201100123409-1960 000002500060000526 AMERICAN
705RMR*11*379477306771000*0000133379
62201100123409-1960 000006656060000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
62201100123409-1960 000024188960000526 AMERICAN
705RMR*11*379477306771000*0000133379
62201100123409-1960 000085263060000526 AMERICAN
8200 03043443 000000000 43000000000000000000000
900000430 3000000000000000 3430000000000000

$ awk '
(/^5/ || /^7/) && t {printf("%s%010d%s\n",substr(s,1,20),t,substr(s,31));t=0}
/^6/ {t+=substr($0,21,10);s=$0;next}
{print}' file

When I use the above script one 6 line is not being added to the total as it appears after 7 line

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 000180434860000526 AMERICAN
705RMR*11*379477306771000*0000133379
62201100123409-1960 000006656060000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000073220360000526 AMERICAN
705RMR*11*379477306771000*0000133379
62201100123409-1960 000085263060000526 AMERICAN
8200 03043443 000000000 43000000000000000000000
900000430 3000000000000000 3430000000000000
$
 
What about this ?
Code:
awk '
function fPrint(){
  if(t)printf("%s%010d%s\n",substr(s,1,20),t,substr(s,31))
  t=0
}
/^5/{fPrint()}
/^6/{t+=substr($0,21,10);s=$0;next}
{print}
END{fPrint()}
' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi
That script works but the only thing is the 6 line for the 2nd group appears at the end of file. This line should be after the 5 line. this is the output. Thank you very much. could you please help.

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
705RMR*11*379477306771000*0000133379
62201100123409-1960 000095171860000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
705RMR*11*379477306771000*0000133379
8200 03043443 000000000 43000000000000000000000
900000430 3000000000000000 3430000000000000
62201100123409-1960 000158483360000526 AMERICAN

required output should be like this

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
705RMR*11*379477306771000*0000133379
62201100123409-1960 000095171860000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000158483360000526 AMERICAN
705RMR*11*379477306771000*0000133379
8200 03043443 000000000 43000000000000000000000
900000430 3000000000000000 3430000000000000



 
Your requirement is confusing... why should the second 6 line be "after the 5 line", but you don't mind if the first 6 line isn't immediately after its corresponding 5 line?

Would it be more accurate to say that they second 6 line should be "before any 8 or 9 lines"? In other words, would this output be fine?

Code:
101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
705RMR*11*379477306771000*0000133379
62201100123409-1960 000095171860000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
705RMR*11*379477306771000*0000133379
62201100123409-1960 000158483360000526 AMERICAN
8200 03043443 000000000 43000000000000000000000
900000430     3000000000000000 3430000000000000

If so you could modify PHV's code to trigger an fPrint() when it encounters a 5, 8 or 9.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top