appsguy606
Programmer
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
$
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
$