i want code in awk with loop to get the sum
TOTAL_AMOUNT=SUM(CLP04)
suppose there are 12 CLP04 segment in my file i want to add upto 5 CLP04 then print next line after BPR segment after calculate the total amount
CLP04 means
ex
CLP*0875299*1*13.79*9.83*0*13*8190615108233B~
CLP*CLP01*CLP02*13.79*CLP04*0*13*8190615108233B~ =9.83
Input file:
BPR*I*TOTAL_AMOUNT*C*CHK************20110916~
LX*1~
TS3*0113528*99*20111231*1*13.79~
CLP*0875299*1*13.79*9.83*0*13*8190615108233B~
CAS*CO*45*3.96~
LX*2~
TS3*0327127*99*20111231*4*835.93~
CLP*0788253*1*657.99*522.76*0*13*8174960108177G~
SVC*N4>00378912498*657.99*522.76**15~
DTM*472*20110817~
CAS*CO*45*135.23~
CLP*0788252*1*126.99*87.6*0*13*8174956108176G~
DTM*472*20110817~
CAS*CO*45*39.39~
CLP*0780207*1*12.06*8.3*0*13*8197106108259G~
DTM*472*20110825~
CAS*CO*45*3.76~
CLP*0778221*1*38.89*37.8*0*13*8195084108256G~
CAS*CO*45*1.09~
LX*3~
TS3*0328511*99*20111231*2*46.28~
CLP*0248208*1*29.29*6.1*0*13*8199996108261G~
CAS*CO*45*23.19~
CLP*0248209*1*16.99*10.75*0*13*8199318108269G~
CAS*CO*45*6.24~
LX*4~
TS3*0714229*99*20111231*1*16.29~
CLP*0435884*1*16.29*8.56*0*13*8170304108160G~
NM1*QC*1*S2AAHUSNV*648BHR3****MI*X18XZ2BBB-TP~
DTM*472*20110825~
CAS*CO*45*3.76~
CLP*0778221*1*38.89*37.8*0*13*8195084108256G~
CAS*CO*45*1.09~
LX*3~
TS3*0328511*99*20111231*2*46.28~
CLP*0248208*1*29.29*6.1*0*13*8199996108261G~
CAS*CO*45*23.19~
CLP*0248209*1*16.99*10.75*0*13*8199318108269G~
CAS*CO*45*6.24~
LX*4~
TS3*0714229*99*20111231*1*16.29~
CLP*0435884*1*16.29*8.56*0*13*8170304108160G~
NM1*QC*1*S2AAHUSNV*648BHR3****MI*X18XZ2BBB-TP~
What operating system and shell are you using?
What have you tried to solve this problem?
Please show us exactly what output you are hoping to produce from your sample input. (Note that the string CLP04
does not appear anywhere in your sample input??? Note that there are no =
characters in your sample input either??? What values are you trying to sum???)
Hello,
I'm not sure to understand your need...
Maybe as:
awk -F'*' 'OFS=FS;/^CLP/{$0=$0"="$5;$2="CLP01";$3="CLP02";$5="CLP04";print}' file
Regards.
1 Like
RudiC
December 24, 2015, 5:36am
4
Summing 5 CLP04 values - assuming that those are $5 in CLP segments/records - is not that difficult:
awk -F* '/^CLP/ {TOT+=$5;CNT++} CNT==5 {print TOT; TOT=CNT=0}' file
666.29
69.31
But, on top of what Don Cragun asked, what to do with the two residual values? And, there's just one "BPR segment" in the first line of the file - should the TOTAL_AMOUNT appear there, in line 2?
1 Like
My OS is AIX 7 and ksh shell
Input data
ISA*00* *00* *ZZ*ABCCOM *ZZ*99999999 *040315*1005*U*00401*004075123*0*P*:
GS*HP*ABCCOM*01017*20110315*1005*1*X*004010X091A1
ST*835*07504123
BPR*H*5.75*C*NON************20110315
CLP*999999*4*25.95*0*25.95*13*0000000555555555*11
CAS*CR*18*25.95*0
REF*6R*999999
CLP*888888*4*162.13*0*162.13*MC*0000000456789123*11
SVC*ZZ:M151000:F0*162.13*0**1.9
DTM*472*20020920
CAS*CO*29*162.13*0*42*0*0
CLP*111111*2*56.52*18.88*0*13*0000000644444444*53
CLP*121212*4*56.52*0*0*13*0000000646464640*53
CLP*333333*1*74.61*59.69*14.92*13*0000000688888888*55
REF*6R*333333
CLP*777777*25*136.9*0*0*13*0000000622222222*53
DTM*472*20030911
CAS*PI*104*136.72*0
CAS*CO*42*0.18*0
NM1*82*2*ABTHSOLUTE HELP*****FI*346608640
REF*F8*A76B04054
SVC*HC:H0004:HF:H9*-86.76*-86.76**-4
DTM*472*20110210
CAS*CR*22*0*0*42*0*0
CAS*OA*22*0*0
REF*6R*090909
LQ*HE*MA92
SE*93*07504123
GE*1*1
IEA*1*004075123
Output File
ISA*00* *00* *ZZ*ABCCOM *ZZ*99999999 *040315*1005*U*00401*004075123*0*P*:
GS*HP*ABCCOM*01017*20110315*1005*1*X*004010X091A1
ST*835*07504123
BPR*H*5.75*C*NON************20110315
CLP*999999*4*25.95*0*25.95*13*0000000555555555*11
CAS*CR*18*25.95*0
REF*6R*999999
CLP*888888*4*162.13*0*162.13*MC*0000000456789123*11
SVC*ZZ:M151000:F0*162.13*0**1.9
DTM*472*20020920
CAS*CO*29*162.13*0*42*0*0
CLP*111111*2*56.52*18.88*0*13*0000000644444444*53
SE*93*07504123
ST*835*07504123
BPR*H*5.75*C*NON************20110315
CLP*121212*4*56.52*0*0*13*0000000646464640*53
CLP*333333*1*74.61*59.69*14.92*13*0000000688888888*55
REF*6R*333333
CLP*777777*25*136.9*0*0*13*0000000622222222*53
DTM*472*20030911
CAS*PI*104*136.72*0
CAS*CO*42*0.18*0
NM1*82*2*ABTHSOLUTE HELP*****FI*346608640
REF*F8*A76B04054
SVC*HC:H0004:HF:H9*-86.76*-86.76**-4
DTM*472*20110210
CAS*CR*22*0*0*42*0*0
CAS*OA*22*0*0
REF*6R*090909
LQ*HE*MA92
SE*93*07504123
GE*1*1
IEA*1*004075123
Thanks in advance
Don CLP04 means input file have number of rows in which row which start with CLP and * is field separator then value after 4th * is CLP 04
And how does adding the three lines:
SE*93*07504123
ST*835*07504123
BPR*H*5.75*C*NON************20110315
after the 3rd CLP
line in your input file relate to your requirements?
Why after 3 CLP
lines (instead of after 5)?
The sum of the CLP04
values on those three CLP
lines (0+0+18.88) is 18.88. Where is that shown in those three added lines?
I still do not understand what you're trying to do.