User
User

Reputation: 411

Parsing errors in awk blocks

awk 'BEGIN 
    { 
    INPUTFILE ='XXX'; iterator =0;
    requestIterator =0;
    storageFlag =T;
    printFlag =F;
    currentIteration =F;
    recordCount =1;
       while (getline < "'"$INPUTFILE"'") 
       { 
             requestArray[requestIterator]++; 
             requestIterator++;
       }
     } 
     if ($1 ~ /RequestId/) 
     {  
            FS = "="; 
            if($2 in requestArray) 
            {
                  storage[iterator] =$0;
                  printFlag =T;
                  next
            }
            else 
            {
                  storageFlag =F;
                  next
            }
      }
      else 
      {
           if((storageFlag =='T' && $0 != "EOE"))   
           {
                storage[iterator]=$0; iterator++;
           } 
           else {if(storageFlag == 'F')
           {
                next
           } 
           else
           {
               if(printFlag == 'T')
               {
                    for(details in storage) 
               {
                    print storage[details] >> FILE1;
                    delete storage[details];
               } 
               printFlag =F;
               storageFlag =T;
               next
           }
     }'

I am facing some syntax error in the above code. Could you ppl please help me?

awk: BEGIN{INPUTFILE =XXXX;iterator =0;requestIterator =0;storageFlag =T;printFlag =F;currentIteration =F;recordCount =1;while (getline < ""){requestArray[requestIterator]++;requestIterator++;}}if ($1 ~ /RequestId/){FS = "=";if($2 in requestArray){storage[iterator] =$0;printFlag =T;next}else{storageFlag =F;next}}else{if((storageFlag ==T && $0 != EOE)){storage[iterator]=$0;iterator++;}else{if(storageFlag == F){next}else{if(printFlag == T){for(details in storage){print storage[details] >> XXXX;delete storage[details];}printFlag = F;storageFlag =T;next}}}}

awk: ^ syntax error

awk: ^ syntax error

Upvotes: 1

Views: 265

Answers (1)

John3136
John3136

Reputation: 29265

Quotes are the problem. The first single quotes on INPUTFILE ='XXX' is going to be parsed as matching the one before BEGIN, and from then on all the parsing is broken.

Either escape the quotes or just put the awk file into a seperate file rather than "inline".

# STARTING POINT - known bad
awk 'BEGIN { INPUTFILE ='XXX'; iterator =0; ... '

Has to be rewritten to remove all of the single quotes inside the outer pair

 awk 'BEGIN { INPUTFILE ="XXX"; iterator =0; ... '

Or depending on if you need doubles or singles, use doubles outside and single inside

awk "BEGIN { INPUTFILE ='XXX'; iterator =0; ... '

or escape the singles quotes so they make it through to awk and don't get consumed by the shell.

awk 'BEGIN { INPUTFILE =\'XXX\'; iterator =0; ... '

All of your problems go away if you put the awk script into a separate file rather than inlining it the shell. You can have whatever quotes you like and no one will care !!

Upvotes: 1

Related Questions