I wrote some Perl that should do the job, as it seemed like a bit much to do in AWK or sed. It constructs a hash of the ranges in B.txt
and prints out any lines from A.txt
, where any one of the values in the comma separated list lies within any of the ranges (currently >=
start and <
end). Note that this was a bit of a learning exercise for me, so I welcome any constructive feedback.
#!/usr/bin/env perl
use strict;
use warnings;
open my $fh,"<","B.txt" or die "couldn't open file: $!";
<$fh>; # skip first line
my @range;
while (<$fh>) {
my @F = split;
push @range, [hex($F[0]), hex($F[0]) + hex($F[1])];
}
close $fh;
open $fh,"<","A.txt" or die "couldn't open file: $!";
while (<$fh>) {
my $match = 0;
OUTER:
for (split ',', (split ';')[1]) {
chomp (my $val = (split '=')[1]);
$val = hex $val;
for my $ref (@range) {
if ($val >= $$ref[0] && $val < $$ref[1]) {
$match = 1;
last OUTER;
}
}
}
print if $match;
}