G'day at2marty,

The following is a potential technique you could use. It uses a single pass through both file1 and file2; those files are not open at the same time.

As others have already pointed out, your question lacks detail. Be aware that I've guessed at the input and have included no checking (e.g. do all records in file1 have at least 4 fields? what to do if they don't? how many fields do records in file2 have? are we appending, replacing, or even extending?). Along with other suggestions, take a look at "How do I post a question effectively?".

Given file1 looks something like:

$ cat pm_11109568_file1.txt A:?:?:A4:? B:?:?:B4:? C:?:?:C4:? D:?:?:D4:?

and file2 looks something like:

$ cat pm_11109568_file2.txt C,?,?,?,?,W6,? X,?,?,D,?,X6,? Y,?,?,?,?,Y6,B Z,?,?,?,?,Z6,?

Running this code:

#!/usr/bin/env perl use strict; use warnings; use autodie; my ($file1, $file2, $file3) = qw{ pm_11109568_file1.txt pm_11109568_file2.txt pm_11109568_file3.txt }; my %search; { open my $fh1, '<', $file1; while (<$fh1>) { my ($field1, $field4) = (split /:/)[0,3]; $search{$field1} = $field4; } } my $re_alt = join '|', keys %search; my $re = qr{(?:^|,)($re_alt)(?:,|$)}; { open my $fh3, '>', $file3; open my $fh2, '<', $file2; while (<$fh2>) { if (/$re/) { my $key = $1; my @fields = split /,/; $fields[5] = $search{$key}; print $fh3 join ',', @fields; } else { print $fh3 $_; } } }

Produces this output file (i.e. file3):

$ cat pm_11109568_file3.txt C,?,?,?,?,C4,? X,?,?,D,?,D4,? Y,?,?,?,?,B4,B Z,?,?,?,?,Z6,?

Just to reinforce what I said at the start, this is just a technique. If it's useful for you, add exception handling, validation, checking, and whatever else you may need (for instance, logging may be appropriate).

— Ken


In reply to Re: regex and printing by kcott
in thread regex and printing by at2marty

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.