简体   繁体   English

在awk中将文件1的一个字段与文件2的另一个字段进行比较

[英]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

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. 对于列出的第一个文件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.

相关问题 awk查找并在匹配时将变量file2替换为file1 - awk find and replace variable file2 into file1 when matched 如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件? - How to append each column of file1 to a specific field of file2 and make a new output file? 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 AWK 根据文件 1 中的部分标题匹配选择文件 2 中的列 - AWK select columns in file2 based on partial header match in file1 如何使用 sed 或 awk 将 file1 的一部分替换为 file2 的一部分,部分由行和列描述 - How to replace a portion of file1 by a portion of file2 uing sed or awk, portion delineated by row and column no 将文件 1 中的字符串与文件 2 中的字符串匹配 - Match string in file1 with string in file2 awk中的Vlookup:如何在输出末尾列出在file2中但不在file1中发生的所有事件? - Vlookup in awk: how to list anything occuring in file2 but not in file1 at the end of output? 使用AWK拆分file1,然后根据file2中的行命名新文件 - splitting file1 using AWK and then name the new files based on lines from file2 Awk 获取file1列并检查file2的column1,如果匹配则打印相应的值 - Awk to get file1 column and check column1 of file2,if matches then print the corresponding value 将File1与File2合并(继续从File1追加到File2,直到不再有行) - Merge File1 with File2 (keep appending from File1 to File2 until no more rows)
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM