在awk中使用数组匹配行

在awk中使用数组匹配行,awk,text-processing,Awk,Text Processing,我试图使用awk来匹配两个文件(file1和file2)。对于file2中与file1匹配的列的每一行,我希望命令打印出file1中的第二列 我在这里查看了几个解决方案,发现了一些有效的方法(部分有效),但我不明白它是如何工作的 awk'NR==FNR{a[$1]=$2;下一个}$1在{print a[$1]}'文件1中的文件2>>输出 以下是输入的示例: #File1 0_1 apple 0_2 mango 0_3 banana ... 3_1 durian 3_4 dra

我试图使用
awk
来匹配两个文件(file1和file2)。对于file2中与file1匹配的列的每一行,我希望命令打印出file1中的第二列

我在这里查看了几个解决方案,发现了一些有效的方法(部分有效),但我不明白它是如何工作的

awk'NR==FNR{a[$1]=$2;下一个}$1在{print a[$1]}'文件1中的文件2>>输出

以下是输入的示例:

#File1
0_1   apple
0_2   mango
0_3   banana
...
3_1   durian
3_4   dragonfruit
3_20  pear
当我将File2的第一列与File1匹配时,上面的awk命令返回我想要的结果

#Output
apple
apple
mango
banana
因此,我自然地对该行进行了一些调整,以便对File2中的第二列执行相同的操作

awk'NR==FNR{a[$1]=$2;下一个}$2在一个{print a[$1]}'文件中1文件2>>输出

但我收到了与上述结果完全相同的结果,而这正是我所期待的:

#Expected output
durian
durian
dragonfruit
pear
更糟糕的是,当我这样做时,我得到了期望的输出:

awk'NR==FNR{a[$1]=$2;下一个}$1在{print a[$2]}'文件1中的文件2>>输出


有人能给我解释一下这背后的逻辑(给数组赋值)吗?或者其他地方出了什么问题吗?

请您对您使用的代码进行以下解释。它可能会帮助您理解数组的概念

awk '                      ##Starting awk program from here.
NR==FNR{                   ##Checking condition FNR==NR which will be TRUE once first Input_file named file1 is being read.
  a[$1]=$2                 ##Creating an array named a whose index is $1 of current line and value is $2(2nd field) of current line.
  next                     ##next will skip all further statements from here.
}                          ##Closing BLOCK for FNR==NR condition here.
$2 in a{                   ##Checking condition(which will be executed only when 2nd Input_file named file2 is being read.
  print a[$1]              ##Now printing value of array a whose index is $1 of current line.
}                          ##Closing BLOCK for $2  in a condition here.
' file1 file2 >> output    ##Mentioning Input_file names and placing output into output file here.
关于阵列概念的补充说明:

  • a[$1]=$2做什么?:这意味着我们正在创建一个名为a的数组,其索引(任何项目都可以通过该索引识别)的值为$2(当前行的第二个字段)
  • 示例
    a[$1]=$2
    让我们以第一个输入文件中的
    0_1 apple
    为例,其中数组将存储为
    a[0_1]=apple
    ,如上所述,其索引为0_1,值为apple
  • 在一个条件中,
    $2做什么?:这个语句实际上是一个条件,它检查当前行的$2是否进入数组a(当然它会检查数组a的所有索引,如果它们匹配或不匹配,则将此字符串与它们进行比较)如果找到任何匹配,则打印值为
    a的数组a的值[$1]

Hi RavinderSingh13,谢谢你的详细回答;下次我试图理解awk命令时,我肯定会将它们分解。我只想再澄清一个问题:当没有
a[$2]时,
打印a[$2]
是什么意思
已分配?它似乎不会直接引用先前分配给数组的值,我对它从a
中的
$1或a
命令中的
$2给出的不同输出集感到困惑。
awk '                      ##Starting awk program from here.
NR==FNR{                   ##Checking condition FNR==NR which will be TRUE once first Input_file named file1 is being read.
  a[$1]=$2                 ##Creating an array named a whose index is $1 of current line and value is $2(2nd field) of current line.
  next                     ##next will skip all further statements from here.
}                          ##Closing BLOCK for FNR==NR condition here.
$2 in a{                   ##Checking condition(which will be executed only when 2nd Input_file named file2 is being read.
  print a[$1]              ##Now printing value of array a whose index is $1 of current line.
}                          ##Closing BLOCK for $2  in a condition here.
' file1 file2 >> output    ##Mentioning Input_file names and placing output into output file here.