使用awk比较两个不同文件中的两列

使用awk比较两个不同文件中的两列,awk,Awk,我有两个文件,如下所示 文件1 文件2 在linux中使用awk的预期输出 已在下面尝试,但未获得预期效果 awk '{k=$1 FS $2} NR!=FNR{a[k]; next} !(k in a)' file1 file2 在linux中使用awk的预期输出 我们必须为两个输入文件设置不同的FS,因为一旦输入文件是管道()分隔的,而另一个则不是,请尝试以下操作。这将只打印那些不在Input_文件2中且存在于Input_文件1中的行 awk 'FNR==NR{a[$0];next} !($

我有两个文件,如下所示

文件1 文件2 在linux中使用awk的预期输出 已在下面尝试,但未获得预期效果

awk '{k=$1 FS $2} NR!=FNR{a[k]; next} !(k in a)' file1 file2
在linux中使用awk的预期输出
我们必须为两个输入文件设置不同的
FS
,因为一旦输入文件是管道(
)分隔的,而另一个则不是,请尝试以下操作。这将只打印那些不在Input_文件2中且存在于Input_文件1中的行

awk 'FNR==NR{a[$0];next} !($1 in a)'  Input_file2  FS="|"  Input_file1
输出如下

1|a
4|d
5|e
6|g
7|h
8|i


解释:为上述命令添加解释

awk '                                       ##Starting awk program from here.
FNR==NR{                                    ##Checking condition FNR==NR which will be TRUE when Input_file2 is being read.
  a[$0]                                     ##Creating an array named a whose index is $0 here.
  next                                      ##next function is awk out of the box function which skips all further statements from here onward.
}                                           ##Closing BLOCK for FNR==NR condition here.
!($1 in a)                                  ##Checking condition if $1 is NOT present in Input_file1, this condition will be executed when Input_file named Input_file1 is being read.
'  Input_file2  FS="|"  Input_file1         ##Mentioning Input_file2 name then setting FS as pipe and mentioning Input_file1 here.

您需要打印文件1中行号不在文件2中的行吗?请告诉我此解决方案是否对您有所帮助?
1|a
4|d
5|e
6|g
7|h
8|i
awk 'FNR==NR{a[$0];next} !($1 in a)'  Input_file2  FS="|"  Input_file1
1|a
4|d
5|e
6|g
7|h
8|i
awk '                                       ##Starting awk program from here.
FNR==NR{                                    ##Checking condition FNR==NR which will be TRUE when Input_file2 is being read.
  a[$0]                                     ##Creating an array named a whose index is $0 here.
  next                                      ##next function is awk out of the box function which skips all further statements from here onward.
}                                           ##Closing BLOCK for FNR==NR condition here.
!($1 in a)                                  ##Checking condition if $1 is NOT present in Input_file1, this condition will be executed when Input_file named Input_file1 is being read.
'  Input_file2  FS="|"  Input_file1         ##Mentioning Input_file2 name then setting FS as pipe and mentioning Input_file1 here.