i am new to perl scripting.. i am still learing it.. i am asked to write a perl script which should compare 2 coloums of 2 different files. if those 2 coloumn are same the script should store the both the lines in 2 diff files.
so i have to compare 3rd coloumn of these to 2 files, eg i have java.lang.string present in both so i want the script store these complete line of both the files in 2 diff files. These 2 files are big files.
please suggest me how can this be done. some one had suggested you can use the hash table implementation.
awk '
# load the contents of file1 into a hash indexed by $3
NR==FNR { file1[$3]=$0; next }
# check whether $3 in file2 is in the hash, if so, print bothlines to files
$3 in file1 { print file1[$3] >> "file1.both"; print >> "file2.both" }
' file1 file2
perl -ane 'BEGIN { open FILE1, ">file1.both"; open FILE2, ">file2.both"; }
if ($. == ++$n) { $h{$F[2]} = $_; close ARGV if eof; next; }
if ($h{$F[2]}) { print FILE1 $h{$F[2]}; print FILE2; }' file1 file2
This isn't very idiomatic Perl, but should hopefully be enough to get you started. The trickery with $n and ARGV is to simulate the awk NR==FNR idiom. The eof thing is to reset line numbers in $. when the file changes; see the eof documentation for a brief discussion.
By the way, $3 in 6515152 52272 * ConstMethodKlass is just "*" -- maybe you want to normalize that, rather than change the script.