bash 比较两个文件并获得相同行的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2836271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compare two files and get the output for the same lines
提问by garconcn
How can I get the following output using linux command by comparing two text files? Thanks.
如何通过比较两个文本文件使用 linux 命令获得以下输出?谢谢。
file1:
文件 1:
site110
site120
file2(Updated):
文件 2(更新):
domain1.com - site110
domain2.com - site111
domain3.com - site112
domain4.com - site113
domain5.com - site120
domain6.com - site1201
domain7.com - site1202
output:
输出:
domain1.com - site110
domain5.com - site120
If I use:
如果我使用:
grep -f file1 file2
the output will be:
输出将是:
domain1.com - site110
domain5.com - site120
domain6.com - site1201
domain7.com - site1202
which the last two lines are not what I want. Thanks.
最后两行不是我想要的。谢谢。
回答by ire_and_curses
From the grep manpage:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
Therefore:
所以:
grep -f file1 file2
domain1.com - site110
domain5.com - site120
回答by Antony.H
回答by jfg956
回答by osgx
May be man paste? Some output processing can be needed.
可能是man paste?可能需要一些输出处理。

