sureshsmr has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks, I just started with perl and seek your advice. I have one large pipe '|' separated text file with many columns. I have another small text file with only one column. I need to write out or extract only those records (lines) from the large file, to a new file, if the value in column 1 matches with the value in column 1 of the smaller file. Here is my code and I am sure it is faulty and so seek your advice :) it is actually returning all lines from File1.txt. But what I need is only those lines where column 1 data is matching. Thanks in advance.
#!c:\perl\bin\perl # Set file paths $FILE1 = "D\:\\Data\\WES\\File1.txt"; $FILE2 = "D\:\\Data\\WES\\File2.txt"; $FILE3 = "D\:\\Data\\WES\\File3.txt"; # print "$FILE1\n"; # print "$FILE2\n"; # print "$FILE3\n"; my %F1hash; open(F1,'<', $FILE1) or die "Can't open $FILE1\n"; open(F2,'<', $FILE2) or die "Can't open $FILE2\n"; open(F3,'>', $FILE3) or die "Can't open $FILE3\n"; print F3 "\n"; # print LOGFILE "$start_date \n"; print F3 "===========================================\n"; print F3 "The following Partnumbers have been synchronized\n"; print F3 "===========================================\n"; print F3 "\n"; my %F1hash = (); while (<F2>) { $ptfkey = $_; $ptfpn = $ptfkey; $ptfpn =~ s/^\s+|\s+$//g; $F1hash{$ptfkey, $ptfpn} = $ptfpn; } close F2; while (<F1>) { #chomp; $uline = $_; @ufields = split(/\|/, $uline); # print "$ufields"; $PartNumber = $ufields[0]; $Std_Cost = $ufields[1]; $Last_Paid_Price = $ufields[2]; $Qty_In_Stock = $ufields[3]; $Moto_Preferred_Part = $ufields[7]; $Rev = $ufields[9]; $Agile_Description = $ufields[10]; if ($PartNumber =~ $Flhash{$PartNumber}) { #print "$ufields[0] \n"; print F3 "$PartNumber|$Std_Cost|$Last_Paid_Price|$Qty_ +In_Stock|$Moto_Preferred_Part|$Rev|$Agile_Description\n"; } } close F1; close F3; exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question on file compare
by choroba (Cardinal) on Mar 13, 2012 at 21:11 UTC | |
|
Re: Question on file compare
by Marshall (Canon) on Mar 13, 2012 at 22:39 UTC | |
by sureshsmr (Initiate) on Mar 14, 2012 at 00:05 UTC | |
by Marshall (Canon) on Mar 14, 2012 at 00:25 UTC | |
by sureshsmr (Initiate) on Mar 14, 2012 at 15:58 UTC | |
|
Re: Question on file compare
by muppetjones (Novice) on Mar 13, 2012 at 21:28 UTC | |
by Marshall (Canon) on Mar 13, 2012 at 23:06 UTC | |
|
Re: Question on file compare
by JavaFan (Canon) on Mar 14, 2012 at 07:49 UTC |