Awk Script Counting of Correct vs. Error Responses

Hello,

I have been trying to use an awk script to parse out correct and incorrect answers in a simple tab-delimited text file. I am trying to compare the user's response to the stimulus presented (in this case, an arrow pointing left or right; e.g., "<--" vs. "-->"). I have the data for the arrow direction and the user's response, which is simply left or right. I then want to tally the total number of correct vs. incorrect responses.

Here is a sample of the data I am trying to analyze:

*** LogFrame Start ***
Task: 1
Procedure: TaskProcImagine
JitterDuration: 8500
Task.Cycle: 2
Task.Sample: 2
Running: Task
CueSwitched: False
Arrow: <--
curPreferred: left
TrialStart.OnsetDelay: 22
TrialStart.OnsetTime: 89521
TrialStart.DurationError: -999999
TrialStart.RTTime: 90080
TrialStart.ACC: 0
TrialStart.RT: 559
TrialStart.RESP: 2
TrialStart.CRESP:
TrialStart.Key1: USER 1 559 2 90080
WaitFeedbackDur: 9441
Choice: left
CustomACC: Error
TotalPoints: 1
LeftFeedback: 0
RightFeedback:
isSwitch: False

The items in bold are the ones I am interested in. In this case, the arrow is pointing left, and the participant's response is left, so that would be a correct choice.

Here is the awk script I have right now:

#Modified by JWB 1/14/2011 for ErrorDiscrim study

######################################################################
# Preprocesses raw output of E-prime task
# ISSUES: 
######################################################################

igawk ' 

BEGIN{

 OFS=" "

    arrow = "-";
    response = "-";
    correct = "0"; 
    error = "0";
    totalpoints = "0";
}


####################### Patterns #############################


#note:  search string may have trouble with newlines as ^M?
{  
    #########  Check for arrows and whether response matches to arrow direction
    if ($1 == "Arrow:") arrow = $2;
    if ($1 == "Choice:") response = $2;
    if ((arrow == "<--" && response == "left") || (arrow == "-->" && response == "right")) correct++;
    else if ((arrow == "<--" && response == "right") || (arrow == "-->" && response == "left")) error++;
  
}



END{
    print "correctChoices = " correct, "incorrectChoices = " error;
}

' $*

However, I get an absurdly high number of correct vs. incorrect choices (1377 and 48, respectively). There should only be about 20 trials total, and most of the responses should be correct. Obviously there is some kind of counting error somewhere in the script, but I have been unable to find it. I have attached the complete data file I am trying to analyze. Any help would be greatly appreciated.

Thanks!

-Andy

How about this?

awk '{gsub("--\>","right");gsub("\<--","left");$0=$0}/Arrow/{x=$2}/Choice/{y=$2;{if(x==y){correct++} else {error++;x=0;y=0}}}
END{print "correctChoices = " correct, "incorrectChoices = " error}' ErrorDiscrim_011011-201-1.txt
correctChoices = 22 incorrectChoices =
1 Like

From a quick peek at your code, it looks like the block of if statements is executing on every single line that's read. And, since they test for every possible combination of arrow and choice, a counter will increment on each line read.

Regards,
Alister

---------- Post updated at 02:07 PM ---------- Previous update was at 01:51 PM ----------

Perhaps something along these lines will work (untested).

#Modified by JWB 1/14/2011 for ErrorDiscrim study

######################################################################
# Preprocesses raw output of E-prime task
# ISSUES: 
######################################################################

igawk ' 

BEGIN{

   OFS=" "
   arrow = ""
   response = ""
   correct = 0
   error = 0
   totalpoints = 0
}


####################### Patterns #############################


#note:  search string may have trouble with newlines as ^M?
/^Arrow:/ { arrow = $2 }
/^Choice:/ {
    response = $2 }
    
    #  Compare arrow to response, increment the appropriate counter, and reset
    if (arrow == "<--" && response == "left") || (arrow == "-->" && response == "right")
        correct++
    else
        error++
    arrow = response = ""
}

END{
    print "correctChoices = " correct, "incorrectChoices = " error;
}

' $*

This solution is essentially the same as yinyuemi's minus the gsubs (sorry, missed that before I started).

Regards,
Alister

Could you give your output it should be? i'm a little confused

Hi, yinyuemi:

In case you're responding to my statement regarding the if statement block incrementing a counter for each line of input, I was referring to the original post.

Regards,
Alister

Thanks, Yinyuemi! That worked just like I wanted it to.

One last question: I have been trying to execute this as a shell script, but the script "hangs" without returning any input. For example:

./analysisScript.awk ErrorDiscrim_201.txt

Leaves everything hanging. However, when I change the infile at the end of the awk script so that it can read in the same infile in the directory, it works fine. How can I modify it to execute as a shell script without hanging?

Thanks again!

-Andy

Hi Andy,

Save the following code as a shell script like script.sh
and run as

./script.sh yourfile
#!/bin/sh
awk '{gsub("--\>","right");gsub("\<--","left");$0=$0}/Arrow/{x=$2}/Choice/{y=$2;{if(x==y){correct++} else {error++;x=0;y=0}}}
END{print "correctChoices = " correct, "incorrectChoices = " error}' $1

Y