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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.