1
votes

I have two files file 1 and file 2. I want to see first column of file1 and go to that line number in file2.After that I am comparing $2 of file1 with $6 of file2. If matches then print "correct ans".I want to do this using awk.

file1.txt

1,B
3,C
2,A

file2.txt

Html,WeB,title,tech,Laravel,B
Html,WeB,title,tech,Laravel,D
Html,WeB,title,tech,Laravel,C

Output.txt

Question  1 is correct
Question 3 is correct
Question 2 is incorrect

I have tried to do this.

awk -F[","] 'NR==FNR{n=$1;b=$2}
  {if( NR==$1 && $6==b){printf "Question n is        correct "}
  else { printf "Question n is incorrect"}}' myfile rahulque
2

2 Answers

1
votes
$ awk -F, 'FNR==NR{ans[NR]=$6;next} {print "Question",$1,"is",((ans[$1]==$2)?"":"in")"correct";}' file2.txt file1.txt
Question 1 is correct
Question 3 is correct
Question 2 is incorrect

How it works

  • FNR==NR{ans[NR]=$6;next}

    For each line of the first file listed, file2.txt, we store the correct answer, $6, in an array ans with the line number, NR, as the key. Then, we tell awk to skip the rest of the commands and jump to the next line.

  • print "Question",$1,"is",((ans[$1]==$2)?"":"in")"correct"

    For every line of the second listed file, file1.txt, we print whether or not the answer for question $1 matches the correct answer specified in array a.

    In more detail, let's look at the ternary statement:

    (ans[$1]==$2)?"":"in"
    

    The answer is correct if ans[$1]==$2. In that case, the ternary statement returns an empty string. If the answer is incorrect, the ternary statement returns the string in. The string returned by the ternary statement is placed in front of the string correct to form the desired word.

0
votes

I always found awk confusing so if I were to find a solution to this question I'd avoid awk all to together and just write a shell script like ...

#!/bin/sh

sort file1.txt | cut -d, -f2 > /tmp/file1.cut
cut -d, -f6 file2.txt > /tmp/file2.cut

count=0
while read -r a && read -r b <&3
do
        count=`expr $count + 1`
        if [ $a == $b ]
        then
                echo  "Question $count is correct"
        else
                echo  "Question $count is incorrect"
        fi
done < /tmp/file1.cut 3< /tmp/file2.cut

... this seems to work with the sample data your provided.

$ cat file1.txt
1,B
3,C
2,A
$ cat file2.txt
Html,WeB,title,tech,Laravel,B
Html,WeB,title,tech,Laravel,D
Html,WeB,title,tech,Laravel,C
$ ./correct.sh
Question 1 is correct
Question 2 is incorrect
Question 3 is correct
$ cat correct.sh
#!/bin/sh

sort file1.txt | cut -d, -f2 > /tmp/file1.cut
cut -d, -f6 file2.txt > /tmp/file2.cut

count=0
while read -r a && read -r b <&3
do
        count=`expr $count + 1`
        if [ $a == $b ]
        then
                echo  "Question $count is correct"
        else
                echo  "Question $count is incorrect"
        fi
done < /tmp/file1.cut 3< /tmp/file2.cut

I sure there is a better way to do this, like using awk, but I think the shell script approach is more flex able. For example if you wanted to count the number of incorrect and correct answers.