Don't know exactly if this is what you were after, but I'm sitting at a hotel before I cross the border and thought I'd slap something together quickly. There are many ways to do this; this was just a quick idea.
It reads in two files (see below), each with unique RC#### and some overlap. It keeps all the individual lines (that have unique RC numbers), and if file2 has one that overlaps, it overwrites the line entry from file1.
Hopefully this is kind of what you were looking for. If not, please clarify a little better. Also note that in file2, it uses the '=' character in the RC number, but I removed that before any comparisons.
#!/usr/bin/perl use 5.12.0; my ( $file1, $file2 ) = qw( 1.txt 2.txt ); my %seen; for my $file ( $file1, $file2 ){ open my $fh, '<', $file or die "Can't open file $file: $!"; while ( my $line = <$fh> ){ chomp; next if $line !~ /RC=?\d{4}\s+/; $line =~ s/RC=/RC/; $line =~ /(RC\d{4})\s+/; my $string = $1 if $1; $seen{ $string } = $line; } close $fh; } for my $key ( keys %seen ){ say $seen{ $key }; }
Output:
This is line RC0003 in file 2. This is line RC0001 in file 2. This is line RC0002 in file 1 This is line RC0004 in file 2.
file1:
This is line RC0001 in file 1 This is line RC0002 in file 1 This is line RC0003 in file 1
file2:
This is line RC=0001 in file 2. This is line RC=0003 in file 2. This is line RC=0004 in file 2.
-stevieb
In reply to Re: Newbie, need help extracting strings from two files and comparing
by stevieb
in thread Newbie, need help extracting strings from two files and comparing
by soulbleach
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |