Best guess at what you want:
file1.txt needle:filed12:field13:field14:field15:field16 haystack:filed22:field23:field24:field25:field26 lost:filed32:field33:field34:field35:field36 file2.txt Line,one,fff,ggg,hhh line,two,hidden,haystack,in,open Line,three,just,some,junk line,four,has,needle,here more,ending,junk.
code:
use strict; use warnings; my @one_lines; open my $one,"<","file1.txt" or die $!; while (<$one>){ chomp; my (@fields)=split /:/; push @one_lines,{KEY=>$fields[0], F4=>$fields[3]}; } close $one; #------ open my $two,"<","file2.txt" or die $!; while (defined(my $line=<$two>)){ chomp $line; my @fields = split /,/,$line; my $matched_line={F4=>""}; for (@one_lines){ next unless $line=~m/$_->{KEY}/; $matched_line=$_; last; } $fields[5] = $matched_line->{F4} if $matched_line->{F4}; print join(",",@fields),"\n"; } close $two;
Output:
>perl 11109568.pl Line,one,fff,ggg,hhh line,two,hidden,haystack,in,field24 Line,three,just,some,junk line,four,has,needle,here,field14 more,ending,junk.
Redirecting output to "file3" is left as an exercise.

                "From there to here, from here to there, funny things are everywhere." -- Dr. Seuss


In reply to Re: regex and printing by NetWallah
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.