Awk bash比较两个完全匹配的列

Awk bash比较两个完全匹配的列,awk,Awk,我正在比较两个文件之间的列以获得精确匹配,但结果不准确。示例如下 File1 File2 adam sunny jhon adam kelly adam matt kevin stuart adam Gary Gary 当我们查看文件时,只有匹配项,即Garry。我的输出应该如下 Emptyline Emptyline Emptyline Emptyline Emptyline Gary 为了达到要求。我正在运行以下命令 awk 'NR==FNR { n[

我正在比较两个文件之间的列以获得精确匹配,但结果不准确。示例如下

File1   File2
adam    sunny
jhon    adam
kelly   adam
matt    kevin
stuart  adam
Gary    Gary
当我们查看文件时,只有匹配项,即Garry。我的输出应该如下

Emptyline
Emptyline
Emptyline
Emptyline
Emptyline
Gary 
为了达到要求。我正在运行以下命令

awk 'NR==FNR { n[$1]=$0;next } ($1 in n) { print n[$1],$2 }' file1 file2
我得到的结果如下

adam
adam
adam
Garry
第一种解决方案:使用简单的
awk

awk 'FNR==NR{a[FNR]=$0;next} a[FNR]==$0{print;next} {print ""}'  file1  file2
或者根据anubhava先生的评论:

awk 'FNR==NR{a[FNR]=$0;next} a[FNR]!=$0{$0=""} 1' file1  file2
解释:添加上述代码的详细解释

awk '              ##Starting awk program from here.
FNR==NR{           ##Checking condition FNR==NR which will be TRUE when first file Input_file1 is being read.
  a[FNR]=$0        ##Creating an array a with index FNR and value of current line here.
  next             ##next will skip all further statements from here.
}
a[FNR]==$0{        ##Checking condition if value of array a with FNR index and current line is equal then do following.
  print $0,a[FNR]  ##Printing current line and value array a with index FNR here.
}
'  file1  file2    ##Mentioning Input_file names here


第二种解决方案:根据所示示例,考虑到您的实际输入文件只有两列,请尝试以下内容

paste Input_file1  Input_file2 | awk '$1==$2{print $1};$1!=$2{print ""}'
此代码仅打印输入文件1和输入文件2中值相等的行。

第一种解决方案:使用simple
awk

awk 'FNR==NR{a[FNR]=$0;next} a[FNR]==$0{print;next} {print ""}'  file1  file2
或者根据anubhava先生的评论:

awk 'FNR==NR{a[FNR]=$0;next} a[FNR]!=$0{$0=""} 1' file1  file2
解释:添加上述代码的详细解释

awk '              ##Starting awk program from here.
FNR==NR{           ##Checking condition FNR==NR which will be TRUE when first file Input_file1 is being read.
  a[FNR]=$0        ##Creating an array a with index FNR and value of current line here.
  next             ##next will skip all further statements from here.
}
a[FNR]==$0{        ##Checking condition if value of array a with FNR index and current line is equal then do following.
  print $0,a[FNR]  ##Printing current line and value array a with index FNR here.
}
'  file1  file2    ##Mentioning Input_file names here


第二种解决方案:根据所示示例,考虑到您的实际输入文件只有两列,请尝试以下内容

paste Input_file1  Input_file2 | awk '$1==$2{print $1};$1!=$2{print ""}'

此代码将只打印输入文件1和输入文件2中值相等的行。

您应该跟踪行号,而不仅仅是行内容:

$ awk 'NR==FNR { lines[NR]=$0; next }
       { if ($0 == lines[FNR]) print; else print "" }' file1.txt file2.txt





Gary

您应该跟踪行号,而不仅仅是行内容:

$ awk 'NR==FNR { lines[NR]=$0; next }
       { if ($0 == lines[FNR]) print; else print "" }' file1.txt file2.txt





Gary

检查
man comm
,可能是您需要的。您当前的方法不正确
comm
确实需要排序文件。如果需要保留订单,它将不起作用。请将您的样本正确地包装在代码标签中,因为它们看起来仍然不太好(在我编辑之后)。@Shawn correct,但是谁知道查询者实际需要什么呢?请检查man comm,可能这是您需要的。您当前的方法不正确
comm
确实需要排序文件。如果需要保留订单,它将不起作用。请将您的样本正确地包装在代码标记中,因为它们看起来仍然不太好(在我编辑之后)。@Shawn correct,但是谁知道查询者实际需要什么awk'FNR==NR{a[FNR]=0;next}a[FNR]=$0{$0=“”}1可能会短一些。谢谢你的夸夸其谈,这对我很有用。我花了一些时间来解决这个问题,但没有运气,再次感谢。@Rao,欢迎你,你可以接受这个答案,然后查看这个链接,一旦你在SO stackoverflow.com/help/someone answers
awk'FNR==NR{a[FNR]=0美元;next}a[FNR]上得到答案,你可以做什么=$0{$0=“”}1
可能会短一些。谢谢你的夸夸其谈,这对我很有用。我花了一些时间来解决这个问题,但是运气不好,再次感谢。@Rao,欢迎您,您可以接受这个答案,然后查看这个链接,一旦您在SO stackoverflow.com/help/someone-answers上得到答案,您可以做些什么