Visit Our UNIX and Linux User Community

Using AWK to Calculate Correct Responses

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using AWK to Calculate Correct Responses
# 1  
Old 05-10-2011
Using AWK to Calculate Correct Responses


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 an error or a correct outcome.

In the IMAGINE condition, I am coding their response as correct if they receive a correct outcome and make the same response on the next trial. I am also coding their response as correct if they receive an error outcome and switch their response on the next trial.

In the RECALL condition, I am coding their response as correct if they make the same response as they did on the previous trial, regardless of whether they received a correct or error outcome on that trial.

Here is a sample of the output I am trying to run through awk:


The data are in groups of three. For example, the first three lines can be thought of as: "Condition = IMAGINE, Response = left, Outcome = correct".

What I want to do is tally how many correct or incorrect switches they made in each condition. In this example, the first response results in a correct outcome, and the subject makes the same response on the next trial, which would be counted as a correct response. On the RECALL trial, the participant also makes a correct response, since the response is the same as the last trial, even though that trial resulted in an error outcome. The last trial is actually an incorrect response, since it is in the IMAGINE condition and the last outcome was correct, and they switched their response.

Is awk the best tool to use for this? My problem is that I need to store the previous response and outcome, and then compare it against the current response and outcome, taking into account which condition it is. Any help would be greatly appreciated.


# 2  
Old 05-10-2011
Can you show us what would be the desired output for this sample data?
# 3  
Old 05-10-2011
Yes; in this example, I would expect the output to be:

IMAGINE condition: totalCorrect = 2, totalIncorrect = 1
RECALL condition: totalCorrect = 1, totalIncorrect = 0


# 4  
Old 05-10-2011
So it is as simple as counting "correct" and "error" words in those two sections? If so, then try this:
awk '{x=$0;getline;getline;if (x=="IMAGINE"){a[$0]++}else{b[$0]++}}END{print "IMAGINE condition: totalCorrect = "a["correct"]", totalIncorect = "a["error"];print "RECALL condition: totalCorrect = "b["correct"]", totalIncorect = "b["error"]}' file

# 5  
Old 05-11-2011
Not quite. "Correct" and "Error" refer to the outcome that they received, not whether they made the right response or not. For example, in the last IMAGINE condition the participant makes an incorrect response; since the last trial resulted in a correct outcome, they should have made the same response.

Let me know if that makes sense!

# 6  
Old 05-12-2011
I think the correct output in this case should be:
IMAGINE condition: totalCorrect = 1 totalIncorect = 1
RECALL condition: totalCorrect = 1 totalIncorect = 0

Because you cannot determine whether first record (IMAGINE) is correct or not, as you don't have previous record to analyze response. Try this script:
open I, "$ARGV[0]";
while ($prev_trial){
  if ($trial eq "IMAGINE"){
    if (($resp eq $prev_resp && $prev_out eq "correct") || ($resp ne $prev_resp && $prev_out eq "error")){
    } else {
  } elsif ($trial eq "RECALL"){
    if ($resp eq $prev_resp){
    } else {
print "IMAGINE condition: totalCorrect = ", ($im_corr)?$im_corr:0, " totalIncorect = ", ($im_inco)?$im_inco:0, "\n";
print "RECALL condition: totalCorrect = ", ($re_corr)?$re_corr:0, " totalIncorect = ", ($re_inco)?$re_inco:0, "\n";

Run it like this: ./ your_file

Previous Thread | Next Thread
Test Your Knowledge in Computers #186
Difficulty: Easy
The primary role of a Moderator at is to enforce the community rules and guidelines.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output is not the correct count

The awk below runs and produces the following output on the file2. This is just an example of the format as the file is ~14MB. file1.txt is attached. I am trying to count the ids that match between the two files and out the ids that are missing. Thank you :). file2 970 NM_213590 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

How to correct this awk code without eval?

Hi everyone, The following piece of awk code works fine if I use eval builtin var='$1,$2' ps | eval "awk '{print $var}'" But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result ps | awk -v v1=$var '{print v1}' # output is $1,$2 ps |... (4 Replies)
Discussion started by: royalibrahim
4 Replies

3. Shell Programming and Scripting

Cannot get the correct ans. Using awk in taking average

Hi all, I think so I’m getting the result is wrong, while using following awk commend, colval=$(awk 'FNR>1 && NR==FNR{a=$4;next;} FNR>1 {a+=$4; print $2"\t"a/3}' filename_f.tsv filename_f2.tsv filename_f3.tsv) echo $colval >> Result.tsv it’s doing the condition 2 times, first result... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

4. Shell Programming and Scripting

Help to get correct data using awk

I have this input.|user1 | |23|046|1726 (212) |0 |user2 | |23|046|43 (17) |0 |test | |23|046|45 (10) |0 |test1 | |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

5. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: Jahn
6 Replies

6. Shell Programming and Scripting

AWK or SED for correct columns

Hello, I have the following file, but one of his columns is not in place, and tried with SED and AWK, how I can correct format? In the second line break is wrong, and puts it after the first column of next line I would appreciate if you could guide me on the subject. (4 Replies)
Discussion started by: nitwh
4 Replies

7. Shell Programming and Scripting

Calculate P Value -Awk

Is there any awk command to calculate P Value ?(Probability) Is it possib;e to calculate P va;ue for this data for ex? 7.891284 8.148193 7.749575 7.958188 7.887702 7.714877 8.141548 7.51845 8.27736 7.929853 7.92456 8.249126 7.989113 8.012573 8.351206 (2 Replies)
Discussion started by: stateperl
2 Replies

8. 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

9. Shell Programming and Scripting

AWK not giving me correct output.

i have a line like this in my script IP=`get_IP <hostname> | awk '{ print $1 }' echo $IP the problem is get_IP <hostname> returns data formated as follows: ip name server_name the code above returns server_name and i just need the I have tried to add "|... (5 Replies)
Discussion started by: mcdef
5 Replies

10. Shell Programming and Scripting

How to calculate with awk

Hi, I have below awk statement and I need to convert the second field ( substr($0,8,6))from minutes to hours with 2 decimail place. How can I achieve this? /usr/bin/awk '{print substr($0,23,4),substr($0,8,6)}' /tmp/MANAGER_LIST.$$ >> /tmp/NEWMANAGER_LIST.$$ Thanks for any help! (4 Replies)
Discussion started by: whatisthis
4 Replies

Featured Tech Videos