We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Using File::Tie to open file grab strng and search for it on different file

warrior32
warrior32 asked
on
Medium Priority
373 Views
Last Modified: 2012-05-07
In perl how can use File::Tie to to open file1 read the first line and search for that string in file2, if found print out "found string" if not found print out "Not found".  Then continue to the second string in file1 searching for it in file2.  So on so forth until no more lines in file1.
I don't want to read the whole file to memory because both of these files might get very large in size.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2009

Commented:
I don't see any need for Tie::File for this, you can just read the file one line at a time, and search for the string.
open(my $fh1, '<file1.txt') or die "could not open file1: $!\n";
open(my $fh2, '<file2.txt') or die "Could not open file2: $!\n";
while(my $s1=<$fh1>) {
	chomp $s1;
	seek $fh2,0,0;
	my $found=0;
	while(<$fh2>) {
		next unless /$s1/;
		print "$s1: found string\n";
		$found=1;
		last;
	}
	print "$s1: Not found\n" unless $found;
}
close($fh1);
close($fh2);

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.