两个文件中的匹配列
我有两个文件,其中第一列可能具有相同的值。我想匹配两个文件的第一列,并打印 FILE1 中匹配的行。
FILE1:
xxx1 yyy yyy yyy
xxx2 yyy yyy yyy
xxx3 yyy yyy yyy
FILE2:
xxx3 zzzz
xxx4 zzzz
OUTPUT:
xxx3 yyy yyy yyy
欢迎任何建议。
最好的祝愿
I have two files, in which the first column of them might have same values. I would like to match the first column of both files, and print the lines in FILE1 for which there was a match.
FILE1:
xxx1 yyy yyy yyy
xxx2 yyy yyy yyy
xxx3 yyy yyy yyy
FILE2:
xxx3 zzzz
xxx4 zzzz
OUTPUT:
xxx3 yyy yyy yyy
Any suggestions are welcomed.
Best wishes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
加入
join
这是我的秘诀:
我假设两个列表均按键(第一列)排序,并且每个键仅在文件中出现一次。第一个模式缩写为:
在这种情况下,默认操作是打印整行。此模式隐式适用于第二个文件 (file1),因为对于第一个文件,密钥尚未标记。
在第二种模式中:
FNR==NR 意味着我们正在处理第一个文件(在本例中为 file2)。在这种情况下,我们标记该密钥以供以后参考。
Here is my recipe:
I assume that both lists are sorted by the key (first column) and each key only appears once in a file. The first pattern short for:
In which case, the default action is to print the whole line. This pattern implicitly works for the second file (file1) only because for the first file, the key has not been marked.
In the second pattern:
The FNR==NR means we are processing the first file (file2 in this case). In this case, we mark the key for later reference.