
Preciso de ajuda no shell script, tenho 2 arquivos grandes com cerca de 1,2 GB de dados, com chave e valores, preciso comparar os dois arquivos com base na chave e armazenar a diferença no valor do terceiro arquivo e o único no arquivo 1,
arquivo 1:
test1 marco;polo;angus
test2 mike;zen;liza
test3 tom;harry;alan
test4 bob;june;janet
arquivo 2:
test1 polo;angus
test2 mike
test4 bob;janet
Gostaria de comparar as duas primeiras colunas do arquivo1 com o arquivo2 (pesquisar todo o conteúdo do arquivo2 nas duas primeiras colunas), se corresponderem, imprimir a diferença de valores. Em seguida, procure a segunda linha do arquivo 1 e assim por diante. Também as chaves exclusivas no arquivo 1 devem ser impressas.
Resultado esperado:
test1 marco
test2 zen;liza
test3 tom;harry;alan
test4 june
Os arquivos que tenho são enormes, contendo cerca de 100.000 linhas, então gostaria de agilizar a execução. Isso está sendo executado em shell script, usando, #!/usr/bin/env bash.
por exemplo:
1332239_44557576_CONTI Lased & Micro kjd $353.50_30062020_lsdf3_no-rule 343323H;343434311H;454656556H;343343432H
É um arquivo de texto simples, com isto como chave ( 1332239_44557576_CONTI Lased & Micro kjd $353.50_30062020_lsdf3_no-rule
) e estes como valores: ( 343323H;343434311H;454656556H;343343432H
)
O arquivo 2 sempre será um subconjunto do arquivo 1, bastando encontrar valores (contra a chave) que não estão presentes no arquivo 2 e únicos no Arquivo 1.
Responder1
Usando script perl:
Supondo que você esteja pesquisandoarquivo2 baseado no arquivo1e não vice-versa. Se você quiser pesquisar o arquivo1 com base no arquivo2 também, você deveadicione outro loop forpara o dicionário file2(hash) .
Arquivos de dados:
$ 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
Roteiro :
#!/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}) );
}
Saída :
$ ./testing.pl file1.txt file2.txt
test1 marco;
test2 zen;liza;
test3 tom;harry;alan
test4 june;
Responder2
Aqui está umestranhoversão que deve ser muito rápida.
Funciona em qualquer coisa que siga o padrão de campo solicitado [string:key][space|";"][string][space|";"] etc.
$ cat file1;echo "";cat file2
test1 marco;polo;angus
test2 mike;zen;liza
test3 tom;harry;alan
test4 bob;june;janet
test1 polo;angus
test2 mike
test4 bob;janet
$ awk -F '[ ;]' '
NR==FNR{ for(i=2;i<=NF;i++){ k[$1,$i]++ } }
NR!=FNR{ for(i=2;i<=NF;i++){ k[$1,$i]++ } }
END{ for(i in k){
if(k[i]==1){
split(i,arr_i,SUBSEP);
k_e[arr_i[1]]=k_e[arr_i[1]]""arr_i[2]";"
}
}
for(i in k_e){
print i" "k_e[i]
}
}' file1 file2 | sort | sed 's/.$//'
test1 marco
test2 liza;zen
test3 harry;alan;tom
test4 june