grep在比较两个文件时缺少一行



我有两个大文件

data.txt(包含1324行email子字符串)

test
test1
test3
test4
test6
test7
test9
test10

values.txt(包含2221行电子邮件:这是包含1324行从上一个)

test@gmail.com
test1@gmail.com
test3@gmail.com
test4@gmail.com
test6@gmail.com
test7@gmail.com
test9@gmail.com
test10@gmail.com
test74@gmail.com
test14@gmail.com
test34@gmail.com
test44@gmail.com
test64@gmail.com
test74@gmail.com

一切正常,问题是我应该有一个包含897行电子邮件的文件,我有874行电子邮件。

所以有23行不见了,我不知道如何找到它们。也许是我的功能出了问题?

grep -v -f data.txt values.txt > result.txt

有没有可能用grep来做呢?

预期result.txt

test74@gmail.com
test14@gmail.com
test34@gmail.com
test44@gmail.com
test64@gmail.com
test74@gmail.com

请尝试以下操作。在GNUawk中编写并使用所示示例进行了测试。考虑到您希望在两个文件中获得公共id。

awk '
FNR == NR{
arr[$0]
next
}
!($1 in arr)
' data.txt  FS="@" values.txt

输出如下:

test74@gmail.com
test14@gmail.com
test34@gmail.com
test44@gmail.com
test64@gmail.com
test74@gmail.com

解释:为以上内容添加详细说明。

awk '                            ##Starting awk program from here.
FNR == NR{                       ##Checking condition which will be TRUE when data.txt is being read.
arr[$0]                        ##Creating arr with index of current line.
next                           ##next will skip all further statements from here.
}
!($1 in arr)                     ##Checking condition if 1st column is NOT present in arr then print line.
' data.txt  FS="@" values.txt    ##Mentioning Input_file names here.

最新更新