[英]Comparing one field of file1 to other field of file2 in awk
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. 我有两个文件,文件1和文件2。我想查看文件1的第一列,并转到文件2中的行号。之后,我要比较文件1的$ 2和文件2的$ 6。 If matches then print "correct ans".I want to do this using awk.
如果匹配,则打印“正确的ans”。我想使用awk执行此操作。
file1.txt FILE1.TXT
1,B
3,C
2,A
file2.txt FILE2.TXT
Html,WeB,title,tech,Laravel,B
Html,WeB,title,tech,Laravel,D
Html,WeB,title,tech,Laravel,C
Output.txt 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
$ 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
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. 对于列出的第一个文件
file2.txt
每一行,我们将正确的答案$6
存储在以行号NR
为键的ans
数组中。 Then, we tell awk to skip the rest of the commands and jump to the next
line. 然后,我们告诉awk跳过其余命令,然后跳到
next
行。
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
. 对于第二个列出的文件
file1.txt
每一行,我们打印问题$1
的答案是否匹配数组a
指定的正确答案。
In more detail, let's look at the ternary statement: 让我们更详细地看一下三元语句:
(ans[$1]==$2)?"":"in"
The answer is correct if ans[$1]==$2
. 如果
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
. 如果答案是不正确,三元语句返回字符串
in
。 The string returned by the ternary statement is placed in front of the string correct
to form the desired word. 三元语句返回的字符串放在
correct
的字符串前面,以形成所需的单词。
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 ... 我总是觉得
awk
令人困惑,因此,如果我想找到这个问题的解决方案,我会避免将awk
全部合并在一起,而只是编写一个shell脚本,例如...
#!/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. 我确定有更好的方法来执行此操作,例如使用awk,但我认为shell脚本方法更具灵活性。 For example if you wanted to count the number of incorrect and correct answers.
例如,如果您要计算不正确和正确答案的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.