シェルスクリプトの助けが必要です。キーと値を含む約1.2 GBのデータを含む2つの大容量ファイルがあります。キーに基づいて2つのファイルを比較し、ファイル1の一意の値と共に3番目のファイルに値を保存する必要があります。違い、
ファイル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の最初の2つの列を比較し(最初の2つの列でfile2の内容全体を検索して)、一致する場合は値の違いを印刷したいと思います。次に、ファイル1の2行目を検索します。ファイル1の一意のキーも印刷する必要があります。
予想出力:
test1 marco
test2 zen;liza
test3 tom;harry;alan
test4 june
私のファイルは約100,000行を含む大容量なので、実行速度を速くしたいと思います。これは、#!/usr/bin/env bash.
次の例を使用してシェルスクリプトで実行されます。
1332239_44557576_CONTI Lased & Micro kjd $353.50_30062020_lsdf3_no-rule 343323H;343434311H;454656556H;343343432H
1332239_44557576_CONTI Lased & Micro kjd $353.50_30062020_lsdf3_no-rule
これは、キー()と値(343323H;343434311H;454656556H;343343432H
)を含む単純なテキストファイルです。
ファイル2は常にファイル1のサブセットであり、ファイル2に存在しない値(キーの場合)とファイル1に固有の値のみを検索します。
ベストアンサー1
Perlスクリプトを使用してください:
検索中だとしましょう。ファイル2はファイル1に基づいています。その逆は真実ではありません。 file2 に基づいて file1 を検索するには、次の操作を行う必要があります。別のforループを追加file2辞書(ハッシュ)の場合。
データファイル:
$ cat file1.txt
test1 marco;polo;angus
test2 mike;zen;liza
test3 tom;harry;alan
test4 bob;june;janet
$ cat file2.txt
test1 polo;angus
test2 mike
test4 bob;janet
スクリプト:
#!/usr/bin/perl
use warnings;
use strict;
my $file1=$ARGV[0];
my $file2=$ARGV[1];
my %dict1; #Stores file1 unique key and value pairs in this dictionary ( HASH in perl )
my %dict2; #Stores file2 unique key and value pairs in this dictionary ( HASH in perl )
my %output; #This is output dictionary after processing all the data to print it out
open(F1,'<',$file1) or die "File not found $file1";
open(F2,'<',$file2) or die "File not found $file2";
#Store both file's contents in %dict1 and %dict2 respectively
while(<F1>)
{
my ($key,$value) = split(/\s+/,$_);
$dict1{$key} = $value;
}
while(<F2>)
{
my ($key,$value) = split(/\s+/,$_);
$dict2{$key} = $value;
}
#Get the unique(difference) value from file2 based in the values in file1
foreach my $k ( keys %dict1 )
{
if ( defined $dict2{$k} )
{
my @dict1values=split(";",$dict1{$k});
my @dict2values=split(";",$dict2{$k});
foreach (@dict1values)
{
if ( $dict2{$k} !~ /[;]*?$_[;]*?/) {
$output{$k} .=$_.";";
}
}
} else {
$output{$k}=$dict1{$k};
}
}
foreach my $ke (sort(keys(%output)))
{
print "$ke $output{$ke}\n" if ( defined($output{$ke}) );
}
出力:
$ ./testing.pl file1.txt file2.txt
test1 marco;
test2 zen;liza;
test3 tom;harry;alan
test4 june;