Visit The New, Modern Unix Linux Community


Awk Script Counting of Correct vs. Error Responses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Script Counting of Correct vs. Error Responses
# 1  
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:

Code:
#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
# 2  
How about this?
Code:
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 =

This User Gave Thanks to yinyuemi For This Post:
# 3  
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).
Code:
#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

Last edited by alister; 02-28-2011 at 03:26 PM..
# 4  
Could you give your output it should be? i'm a little confused
# 5  
Quote:
Originally Posted by yinyuemi
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
# 6  
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

Last edited by Jahn; 02-28-2011 at 04:22 PM..
# 7  
Hi Andy,

Save the following code as a shell script like script.sh
and run as
Code:
./script.sh yourfile

Code:
#!/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

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #939
Difficulty: Easy
The author named the ping utility after the sound that a sonar makes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Error in script while counting processes

Hi all, Below is a script I'm writing and giving me error: #!/usr/bin/sh if ; then echo "Success!" else echo "Failure!" fi Normally if I do ps -ef|grep dw.sap|wc -l it gives me output of 18. So my script checks if it's greater than 17 it echoes success else failure ... (5 Replies)
Discussion started by: frum
5 Replies

2. Shell Programming and Scripting

Shell script with awk command for counting in a file

Hi, I hope you can help me with the awk command in shell scripting. I want to do the following, but it doesn't work. for i in $REF1 $REF2 $REF3; do awk '{if($n>=0 && $n<=50000){count+=1}} END{print count}' ${DIR}${i} >${DIR}${i}_count.txt done REF1 to REF3 are only variables for .txt... (1 Reply)
Discussion started by: y.g.
1 Replies

3. Shell Programming and Scripting

Help to get correct data using awk

I have this input.|user1 |10.10.10.10 |23|046|1726 (212) |0 |user2 |10.10.10.11 |23|046|43 (17) |0 |test |10.10.10.12 |23|046|45 (10) |0 |test1 |10.10.10.13 |23|046|89 (32) |0 I need to get the data for a user like thisuser1 1726 user2 43 test 45 test1 89... (11 Replies)
Discussion started by: Jotne
11 Replies

4. Shell Programming and Scripting

[AWK script]Counting the character in record and print them in condition

.......... (1 Reply)
Discussion started by: Antonlee
1 Replies

5. Shell Programming and Scripting

Help with awk array syntax & counting script

..... (3 Replies)
Discussion started by: elbee11
3 Replies

6. Shell Programming and Scripting

Can anyone correct the error in this script

ret=`sqlplus -s /nolog << EOF connect $db_user/$db_pwd@$db_sid; SPOOL ./$DirectoryName/TableData.txt; set pagesize 0 feedback off verify off heading off echo off linesize 150 while read var_ack_party_name do select * from bus_event where ack_party_name like... (13 Replies)
Discussion started by: rkrish
13 Replies

7. Shell Programming and Scripting

Please correct the error in the following script

#!/bin/ksh db_user=$DB_USER_NAME db_pwd=$DB_PASSWORD db_sid=$TWO_TASK if ; then echo "\tUsage: MoveUsageProcessing <BC Log file Name>" exit 1 else BCLogFileName=$1 fi grep -i 'MoveUsage daemon needs to run on this account before it can be billed' $1 |awk -F\| '{for(i=0;++i<=NF;) if($i ~... (1 Reply)
Discussion started by: Rajesh Putnala
1 Replies

8. Shell Programming and Scripting

counting using awk

Hi, I want to perform a task using shell script. I am new to awk programming and any help would be greatly appreciated. I have the following 3 files (for example) file1: Name count Symbol chr1_1_50 10 XXXX chr3_101_150 30 YYYY File2: Name ... (13 Replies)
Discussion started by: Diya123
13 Replies

9. Shell Programming and Scripting

Using AWK to Calculate Correct Responses

Hello, I am trying to count how many times a subject makes a correct switch or a correct stay response in a simple task. I have data on which condition they were in (here, labeled "IMAGINE" and "RECALL"), as well as whether they made a left or right button response, and whether the outcome was... (5 Replies)
Discussion started by: Jahn
5 Replies

10. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

Featured Tech Videos