Awk 将列匹配到一个单独的文件并将匹配项追加到文件
我正在尝试使用awk合并在单个列上过滤的两个文件。然后我想做的是将文件2中的相关列附加到文件1中 使用虚拟示例更容易解释 文件1 文件2:Awk 将列匹配到一个单独的文件并将匹配项追加到文件,awk,Awk,我正在尝试使用awk合并在单个列上过滤的两个文件。然后我想做的是将文件2中的相关列附加到文件1中 使用虚拟示例更容易解释 文件1 文件2: animal number shape cat eight square dog nine circle mouse eleven sphere 期望输出: name fruit animal shape bob apple dog circle jim orange ca
animal number shape
cat eight square
dog nine circle
mouse eleven sphere
期望输出:
name fruit animal shape
bob apple dog circle
jim orange cat square
gary mango snake NA
daisy peach mouse sphere
步骤1:需要对文件1中的第3列和文件2中的第1列进行筛选
awk-F'\t''NR==FNR{c[$3]++;next};c[$1]>0'文件1文件2
这给了我输出:
cat eight square
dog nine circle
mouse eleven sphere
这对我有些帮助,但是我不能简单地从上面的输出中剪切第三列(形状)并将其附加到file1,因为file2中没有“snake”条目。我需要能够将输出的第3列附加到文件1中,在该文件中匹配成功,并且不能放置“NA”。必须保留文件1中的所有行,这样我就不能忽略它们。这就是我被困的地方
我非常感谢您的帮助。。。。
E请您尝试以下内容,并根据GNU
awk
中显示的样本编写和测试
awk '
BEGIN{
OFS="\t"
}
FNR==NR{
a[$1]=$NF
next
}
{
print $0,($3 in a?a[$3]:"NA")
}' Input_file2 Input_file1
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
OFS="\t" ##Setting TAB as output field separator here.
}
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when first Input_file file2 is being read.
a[$1]=$NF ##Creating array a with index $1 and value is $NF for current line.
next ##next will skip all further statements from here.
}
{
print $0,($3 in a?a[$3]:"NA") ##Printing current line and checking if 3rd field is present in array a then print its value OR print NA.
}' file2 file1 ##Mentioning Input_file names here.
你是个英雄!非常感谢。我现在应该能够找出如何处理100列和30000行所需的内容:-D@lecb,您在这个伟大的网站上的欢迎和愉快的学习欢呼:)我已经将同样的方法应用到了更大的文件大小上,而且似乎是偶然的。当确实存在匹配项时,我得到了一些“NA”(我交叉检查了匹配项是否存在于两个文件和正确的列中)。不知道那里发生了什么事!不幸的是,我们无法共享数据。如果,在我上面的例子中,当有匹配时,我想从file2打印数字和形状呢?!在上面,我已经解决了这个问题。awk脚本仅在与您要匹配的列(在文件1中)前面的列为非空时才起作用。我设法使用:
awk'BEGIN{FS=OFS=“\t”}{for(i=1;i@lecb如果您的输入是以制表符分隔的,您应该在问题中说明。如果某些字段可能为空,您应该在问题的示例中包含该字段。我们所要做的就是帮助您了解您的问题。请在中编写这两个脚本-您在第一个脚本中发现必须将FS设置为\t
,并且然后,您忘记在第二个块中执行此操作,您正在读取NR==FNR块中的file2,因此将$2与$NF一起保存在那里。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
OFS="\t" ##Setting TAB as output field separator here.
}
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when first Input_file file2 is being read.
a[$1]=$NF ##Creating array a with index $1 and value is $NF for current line.
next ##next will skip all further statements from here.
}
{
print $0,($3 in a?a[$3]:"NA") ##Printing current line and checking if 3rd field is present in array a then print its value OR print NA.
}' file2 file1 ##Mentioning Input_file names here.