I know that there are a couple of other posts out there but I have been unable to decipher enough to help me on my issue. I basically have two text files that have fields in them and I want to match on those fields to generate a third text file when the fields match. So for example
File 01School District State,School District Name, School District Key, Number of Students, Grade AK, Juneau School District, 21, 100, 4 AK, Juneau School District, 21, 98, 2
File 02School District State,School District Name, School District Key, Grade, Race, CountofRace AK, Juneau School District, 21, 4, I, 12 AK, Juneau School District, 21, 2, P, 47
File 03 (Output File)School District State,School District Name, School District Key, Number of Students, Grade, Race, CountofRace AK, Juneau School District, 100, 21, 4, I, 12 AK, Juneau School District, 98, 21, 2, P, 47
The following only loops through the first record of the first file and then loops through all the records in the second file. What I would like to do is have the first file loop through all the records in the second file and output to the third file and then do the second record in the first file and on until it goes through all the records and checks them against the second file:#! /usr/bin/perl $file1path = "c:/Documents and Settings/bob/My Documents/Research/"; $file1name = "AK_Schools_Student_Race_By_Grade_2010091001.csv"; $file1= $file1path.$file1name; $file2path= "c:/Documents and Settings/bob/My Documents/Research/"; $file2name = "AK_School_Student_Pop_By_Grade_2010091001.csv"; $file2=$file2path.$file2name; $file3path= "c:/Documents and Settings/bob/My Documents/Research/Testi +ng"; $file3name = "Combined_File.csv"; $file3 = $file3path.$file3name; $counter = 0; $counter2 = 0; open (f1, $file1) ; open (f2, $file2) ; open (f3 , ">>$file3"); open( f4, ">>c:/Documents and Settings/bob/My Documents/Research/Testi +ng/looping_file.csv"); while (<f1>) { chomp; @f1_array=split (",") ; $f1_state = @f1_array[0]; $f1_district_name =@f1_array[1]; $f1_district_key = @f1_array[2]; $f1_school_name=@f1_array[3]; $f1_school_key=@f1_array[4]; $f1_site_guid=@f1_array[5]; $f1_student_grade=@f1_array[6]; $f1_count_of_students=@f1_array[7]; $counter=$counter+1; #print "$f1_state , \n"; #print "$f1_district_name , \n"; #print "$f1_district_key , \n"; #print "File 01 - $counter, \n"; while (<f2>) { chomp; @f2_array=split (","); $f2_state = @f2_array[0]; $f2_district_name =@f2_array[1]; $f2_district_key = @f2_array[2]; $f2_school_name=@f2_array[3]; $f2_school_key=@f2_array[4]; $f2_site_guid=@f2_array[5]; $f2_student_grade=@f2_array[6]; $f2_student_race=@f2_array[7]; $counter2=$counter2+1; $f2_student_race_count=@f2_array[8 +]; #print f4 "$f2_state , "; #print f4 "$f1_state , \n"; #print f4 "$f2_district_name , "; #print f4 "$f1_district_name , \n"; #print f4 "File 02 District Key - $f2_district_key , "; #print f4 "File 01 District Key - $f1_district_key, \n"; #print f4 "$f2_school_name, "; #print f4 "$f1_school_name, \n"; #print f4 "File 01 - $counter,"; #print f4 "File 02 - $counter2, \n"; } if (($f1_state eq $f2_state) && ($f1_district_key eq $f2_d +istrict_key)) { print f3 "$f1_state , "; print f3 "$f1_district_name ,"; print f3 "$f2_district_name ,"; print f3 "$f1_district_key ,"; print f3 "$f2_district_key ,"; print f3 "$f1_school_name , "; print f3 "$f1_school_key , "; print f3 "$f1_site_guid ,"; print f3 "$f1_student_grade , "; print f3 "$f1_count_of_students ,"; print f3 "$f2_student_race , \n"; } } close (f1); close (f2);
In reply to Compare Two Text Files Based on a String by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |