简体   繁体   English

连接文件并创建索引

[英]join files and create index

I want to join two files and index them in a new column as show below : 我想加入两个文件并在新列中为它们建立索引,如下所示:

file A 文件A

apple     1 2 3 4 5 6
banana    3 2 4 4 5 6
orange    2 3 4 5 6 7
pear      2 4 5 6 3 5

file B 文件B

apple    1 3 4 5 6 7
grapes   4 5 6 4 3 6
melon    3 4 5 2 5 1
orange   2 4 5 6 7 8

I want to compare the two files based on first two columns, and output the common rows from file A and then add unique rows from both file A and file B and index them as shown below 我想比较基于前两列的两个文件,并输出文件A的公共行,然后添加文件A和文件B的唯一行并为其编制索引,如下所示

output: 输出:

apple     1 2 3 4 5 6 both
orange    2 3 4 5 6 7 both
banana    3 2 4 4 5 6 fileA
pear      2 4 5 6 3 5  fileA
grapes   4 5 6 4 3 6   fileB
melon    3 4 5 2 5 1   fileB

comm compares two files and reports which lines occur in one, the other or both: comm比较两个文件,并报告哪些行出现在一个,另一个或两个行中:

comm -2 <(cut -f1 -d' ' fileA | sort)
        <(cut -f1 -d' ' fileB | sort) \
  | sed $'/\t/{s/$/ both/;s/\t//};/ /!s/$/ fileA/' \
  | join -o1.1,2.2,2.3,2.4,2.5,2.6,2.7,1.2 - fileA \
  | sort -k8
cut -f1 -d' ' fileA \
  | grep -vFf- fileB \
  | sed 's/$/ fileB/'

As you can see, the pipeline is pretty complicated. 如您所见,管道非常复杂。 If you plan to make changes to the code, consider moving to a more powerful language like Perl. 如果您打算更改代码,请考虑使用功能更强大的语言,例如Perl。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM