Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, Can anyone help me with this. There are two files and I have to match the second file records with that of first and if matched, print the output in two fies, one containing the matched records and other containing the rest. Here is the example.
File1 "111yu",erter,"00000", ,"567" "gh6fr",erter,"00000", ,"567" "1t76y",erter,"00000", ,"yu7" "yu84e",erter,"00000", ,"5h7" -- -- --
file2 111yu 345cv yu84e -- --
As can be seen 111 and yu8 matches with the first record in file 1, so the whole row should be printed. Output should be Code
file3 "111yu",erter,"00000", ,"567" "yu84e",erter,"00000", ,"5h7" -- -- and file4 "gh6fr",erter,"00000", ,"567" "1t76y",erter,"00000", ,"yu7" --- ---
Note that the input files are quite big. - Please help... Thanks.

Replies are listed 'Best First'.
Re: file comparison and splitting
by GrandFather (Saint) on May 13, 2008 at 04:14 UTC

    What have you tried and where did you get stuck? Show us a little sample code and explain what's not working. Note that you can simulate files using strings for test purposes. Consider:

    use strict; use warnings; my $file1 = <<'STR'; "111yu",erter,"00000", ,"567" "gh6fr",erter,"00000", ,"567" "1t76y",erter,"00000", ,"yu7" "yu84e",erter,"00000", ,"5h7" STR my $file2 = <<'STR'; 111yu 345cv yu84e STR for my $file (\$file1, \$file2) { open my $inFile, '<', $file; print while <$inFile>; close $inFile; }

    Prints:

    "111yu",erter,"00000", ,"567" "gh6fr",erter,"00000", ,"567" "1t76y",erter,"00000", ,"yu7" "yu84e",erter,"00000", ,"5h7" 111yu 345cv yu84e

    Perl is environmentally friendly - it saves trees
Re: file comparison and splitting
by pc88mxer (Vicar) on May 13, 2008 at 04:32 UTC