M_ID,M_NAME,DEPT_ID,START_DATE,END_DATE,Salary
M001,Richa,D001,27-Jan-07,27-Feb-07,150000
M002,Nitin,D002,16-Feb-07,16-May-07,40000
M003,AJIT,D003,8-Mar-07,8-Sep-07,70000
M004,SHARVARI,D004,28-Mar-07,28-Mar-08,120000
M005,ADITYA,D002,27-Apr-07,27-Jul-07,40000
M006,Rohan,D004,12-Apr-07,12-Apr-08,130000
M007,Usha,D003,17-Apr-07,17-Oct-07,70000
M008,Anjali,D002,2-Apr-07,2-Jul-07,40000
M009,Yash,D006,11-Apr-07,11-Jul-07,85000
M010,Nalini,D007,15-Apr-07,15-Oct-07,9999
awk -F\, -v sum=0 -v sum1=0 '{if($3=="D003"){sum=sum+$6} elseif ($3=="D004") {sum1=sum1+$6}} END{print sum,sum1}' Joining_date.txt
awk: line 1: syntax error at or near {
awk: line 1: syntax error at or near }
答案1
没有elseif
in awk
,您需要使用else if
:
$ awk -F, -v sum=0 -v sum1=0 '{if($3=="D003"){sum=sum+$6} else if ($3=="D004") {sum1=sum1+$6}} END{print sum,sum1}' file
140000 250000
或者,更清晰一点:
awk -F, -v sum=0 -v sum1=0 '{
if($3=="D003"){
sum=sum+$6
}
else if ($3=="D004") {
sum1=sum1+$6
}
} END{
print sum,sum1
}' file