bash 两个文本文件的内连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13272717/
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
Inner join on two text files
提问by Dave Snigier
Looking to perform an inner join on two different text files. Basically I'm looking for the inner join equivalent of the GNU join program. Does such a thing exist? If not, an awkor sedsolution would be most helpful, but my first choice would be a Linux command.
希望对两个不同的文本文件执行内部联接。基本上,我正在寻找与 GNU 连接程序等效的内部连接。这样的事情存在吗?如果没有,awkorsed解决方案将是最有帮助的,但我的首选将是 Linux 命令。
Here's an example of what I'm looking to do
这是我想要做的一个例子
file 1:
文件 1:
0|Alien Registration Card LUA|Checklist Update
1|Alien Registration Card LUA|Document App Plan
2|Alien Registration Card LUA|SA Application Nbr
3|Alien Registration Card LUA|tmp_preapp-DOB
0|App - CSCE Certificate LUA|Admit Type
1|App - CSCE Certificate LUA|Alias 1
2|App - CSCE Certificate LUA|Alias 2
3|App - CSCE Certificate LUA|Alias 3
4|App - CSCE Certificate LUA|Alias 4
file 2:
文件2:
Alien Registration Card LUA
Results:
结果:
0|Alien Registration Card LUA|Checklist Update
1|Alien Registration Card LUA|Document App Plan
2|Alien Registration Card LUA|SA Application Nbr
3|Alien Registration Card LUA|tmp_preapp-DOB
采纳答案by choroba
Should not the file2 contain LUAat the end?
file2 不应该包含LUA在最后吗?
If yes, you can still use join:
如果是,您仍然可以使用join:
join -t'|' -12 <(sort -t'|' -k2 file1) file2
回答by glenn Hymanman
Looks like you just need
看起来你只需要
grep -F -f file2 file1
回答by ghoti
Here's an awk option, so you can avoid the bash dependency (for portability):
这是一个 awk 选项,因此您可以避免 bash 依赖项(为了可移植性):
$ awk -F'|' 'NR==FNR{check[cat file2 | while read line; do
    grep $line file1 # or whatever you want to do with the $line variable
done
];next}  in check' file2 file1
How does this work?
这是如何运作的?
- -F'|'-- sets the field separator
- 'NR==FNR{check[$0];next}-- if the total record number matches the file record number (i.e. we're reading the first file provided), then we populate an array and continue.
- $2 in check-- If the second field was mentioned in the array we created, print the line (which is the default action if no actions are provided).
- file2 file1-- the files. Order is important due to the- NR==FNRconstruct.
- -F'|'-- 设置字段分隔符
- 'NR==FNR{check[$0];next}-- 如果总记录数与文件记录数匹配(即我们正在读取提供的第一个文件),那么我们填充一个数组并继续。
- $2 in check-- 如果我们创建的数组中提到了第二个字段,则打印该行(如果未提供任何操作,则这是默认操作)。
- file2 file1- 文件。由于- NR==FNR构造,顺序很重要。
回答by hcg
You may modify this script:
您可以修改此脚本:
paste [option] source files [>destination file]
while loop reads file2 line by line and gives that line to the grep command that greps that line in file1. There're some extra output that maybe removed with grep options.
while 循环逐行读取 file2 并将该行提供给 grep 命令,grep file1 中的该行。有一些额外的输出可能会被 grep 选项删除。
回答by hcg
You can use paste command to combine file :
您可以使用 paste 命令来合并文件:
paste file1.txt file2.txt >result.txt
for your example it would be
对于您的示例,它将是
##代码##
