0
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 }
0

1 Answer 1

5

There is no elseif in awk, you need to use 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

Or, a bit more legible:

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
2
  • yes..i could work with this modification.. beauty part looks good too 👍 but I'm new to Linux so still not used to writing this..anyway..very huge thanks
    – Faisal
    Sep 22 at 12:25
  • You're welcome, @Faisal. And don't worry, we were all new once! By the way, if this or any other answer you receive solves your issue, please take a moment and accept it by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
    – terdon
    Sep 22 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.