我正在尝试比较两个文件中的数据,其中一个文件包含列,然后希望打印在两个文件中找到的数据
文件一:
item1
item2
item3
文件2:
itemA item1
itemB item2
itemC item3
因此,我想将文件 1 与文件 2 的第 2 列进行比较,如果它们相同,我想显示整行。
例如如果文件1包含:
data1
data2
和文件2包含:
dataA data1
dataC data3
只会显示:
dataA data1
因为这是 file2 中的行,其中包含 file1 中的数据项
提前谢谢了
答案1
使用grep
:
grep -Fwf file1 file2
从man grep
:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by
newlines, any of which is to be matched.
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with
the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and
therefore matches nothing.
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching
substring must either be at the beginning of the line, or preceded by a non-word constituent
character. Similarly, it must be either at the end of the line or followed by a non-word
constituent character. Word-constituent characters are letters, digits, and the underscore.
或者awk
:
awk 'NR==FNR{seen[$0]++} ($2 in seen)' file1 file2
在上面,首先我们正在阅读文件1并将整个column1保存到名为的数组中见过,然后查看 file2 的第二列,如果它与 file1 中保存的 column1 匹配,则打印 file2 的整行。
join
如果两个文件都已排序,您还可以使用命令(如果没有,您可以通过sort
ing 传递排序的输出):
join -1 1 -2 2 file1 file2
-1 FIELD
join on this FIELD of file 1
-2 FIELD
join on this FIELD of file 2
如果文件未排序:
join -1 1 -2 2 <(sort file1) <(sort file2)