[英]Extract lines from File2 already found File1
Using linux commandline, i need to output the lines from text file2 that are already found in file1 . 使用linux命令行,我需要从file1中已经找到的文本file2中输出行。
File1 : 文件1 :
C
A
G
E
B
D
H
F
File2 : 文件2 :
N
I
H
J
K
M
D
L
A
Output : 输出 :
A
D
H
Thanks! 谢谢!
You are looking for the tools 'grep' 您在寻找“ grep”工具
Check this out. 看一下这个。
Lets say you have inputs in file1
& file2
files 假设您在file1
和file2
文件中有输入
grep -f file1 file2
will return you 会回报你
H
D
A
A more flexible tool to use would be awk
使用更灵活的工具将是awk
awk 'NR==FNR{lines[$0]++; next} $1 in lines'
Example 例
$ awk 'NR==FNR{lines[$0]++; next} $1 in lines' file1 file2
H
D
A
What it does? 它能做什么?
NR==FNR{lines[$0]++; next}
NR==FNR
checks if the file number of records is equal to the overall number of records. NR==FNR
检查记录的文件数是否等于记录的总数。 This is true only for the first file, file1
这仅对于第一个文件file1
是正确的
lines[$0]++
Here we create an associative array with the line, $0
in file 1 as index. lines[$0]++
在这里,我们创建一个关联数组,其中行1中的$0
作为索引。
$0 in lines
This line works only for the second file because of the next
in previous action. $0 in lines
由于上next
操作中的next
一个操作,该行仅适用于第二个文件。 This checks if the line in file 2 is there in the saved array lines
, if yes the default action of printing the entire line is taken 这将检查文件2中的lines
是否在保存的数组lines
,如果是,则执行打印整行的默认操作
Awk
is more flexible than the grep
as you can columns in file1 with any column in file 2 and decides to print any column rather than printing the entire line Awk
比grep
更加灵活,因为您可以在file1中的列与文件2中的任何列一起使用,并决定打印任何列而不是打印整行
这是comm
实用程序的工作,但是您必须首先对文件进行排序:要获得2个文件之间的共同点:
comm -12 <(sort File1) <(sort File2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.