bash 如果第一个文件/bash脚本中存在,则通过键合并两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10364455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:09:02  来源:igfitidea点击:

merge two files by key if exists in the first file / bash script

filebashunixjoinmerge

提问by Gregory Maris

i have two files with columns sorted by the value of the first column, and i want to merge them only if the value of the second exists in the first one.

我有两个文件,其中的列按第一列的值排序,并且仅当第二个值存在于第一个列中时,我才想合并它们。

The first file is like this

第一个文件是这样的

man01 xxx yyy zzz
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

The second file

第二个文件

man01 sss
man08 sss

And the desired output is

所需的输出是

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

I tried join but requires values of second file exist in the first one :/

我试过 join 但需要第一个文件中存在第二个文件的值:/

回答by Kai Sternad

Join can do this, have you considered the -aoption ? It will produce a line for each unpairable file line in a.txt and b.txt.

加入可以做到这一点,你考虑过这个-a选择吗?它将为 a.txt 和 b.txt 中每个不可配对的文件行生成一行。

join -a1 a.txt b.txt

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

回答by thb

Well, it ain't a beauty of elegant command-line usage, but what about this?

好吧,它不是优雅的命令行使用的美,但是这个呢?

perl -we 'open F1, "<file1.txt"; open F2, "<file2.txt"; my %f2 = map {/(\S+)/ ? (=>$_) : ()} <F2>; while (<F1>) { chomp; print; if (/(\S+)/ && exists $f2{}) { $f2{} =~ /\S+\s+(.*)/ and print " "; } print "\n"; }'

Update:While I was working on the above, it seems as though @bunting and @c00kiemon5ter came up with a much more elegant answer. Excellent!

更新:当我在做上述工作时,似乎@bunting 和@c00kiemon5ter 想出了一个更优雅的答案。优秀!