Bash 根据键比较两个文件,在另一个文件中打印值的差异

Bash 根据键比较两个文件,在另一个文件中打印值的差异,bash,shell,sh,ksh,Bash,Shell,Sh,Ksh,我有两个大文件,大约1.2GB的数据,带有键和值,我需要根据键比较两个文件,并将值的差异存储在第三个文件中 文件1: test1 marco;polo;angus test2 mike;zen;liza test3 tom;harry;alan test4 bob;june;janet 文件2: test1 polo;angus test2 mike test4 bob;janet 我想比较file1和file2的前两列,如果它们匹配,则在前两列中搜索file2的全部内容,并打印值的差异。然

我有两个大文件,大约1.2GB的数据,带有键和值,我需要根据键比较两个文件,并将值的差异存储在第三个文件中

文件1:

test1 marco;polo;angus
test2 mike;zen;liza
test3 tom;harry;alan
test4 bob;june;janet
文件2:

test1 polo;angus
test2 mike
test4 bob;janet
我想比较file1和file2的前两列,如果它们匹配,则在前两列中搜索file2的全部内容,并打印值的差异。然后搜索文件1的第二行,依此类推。还应打印文件1中唯一的密钥

预期产出:

test1 marco
test2 zen;liza
test3 tom;harry;alan
test4 june
我拥有的文件非常庞大,大约包含100000行,因此我想加快执行速度。

一种方法:

$ comm -23 <(perl -lane 'print "$F[0]\t$_" for split /;/, $F[1]' file1 | sort) \
           <(perl -lane 'print "$F[0]\t$_" for split /;/, $F[1]' file2 | sort) | \
  datamash -g1 collapse 2 | tr ',' ';'
test1   marco
test2   liza;zen
test3   alan;harry;tom
test4   june
或者,使用纯perl版本将整个第二个文件加载到内存中:

!/usr/bin/env perl 以perl whatever.pl文件1文件2的形式运行 使用警告; 严格使用; 使用自动模具; 使用功能qw/say/; 我的%价值观;
打开我的$file2,内存使用是一个问题吗?另外,我删除了bash、ksh和sh标记。重新添加您实际使用的任何一个。感谢您的回复,我是shell脚本的新手,在运行命令时遇到以下错误,-bash:perl:command-not-found-bash:perl:command-not-found-bash:datamash:command-not-found-bash:perl:command-not-found-bash:perl:command-not-found comm:'/dev/fd/62':没有这样的文件或目录,这是在shell脚本中运行的,使用/usr/bin/env bash的真实条目如下所示:1332239_445575776_CONTI Lased&Micro kjd$353.50_3006202_lsdf3_无规则343323H;34311H;454656556H;343343232hn错误消息bash:perl:command-notfound并没有多少神秘之处。您试图从bash调用命令perl,但在您的路径中找不到该命令。perl不是一个标准的UNIX工具,因此您可能必须在平台上安装它。
comm -23 <(perl -lane 'print "$F[0]\t$_" for sort split(/;/, $F[1])' file1) \
         <(perl -lane 'print "$F[0]\t$_" for sort split(/;/, $F[1])' file2) | \
 datamash -g1 collapse 2 | tr ',' ';'