in reply to regex and printing

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