Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: regex and printing

by NetWallah (Canon)
on Dec 03, 2019 at 06:02 UTC ( [id://11109578]=note: print w/replies, xml ) Need Help??


in reply to regex and printing

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-26 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found