in reply to searching string from one file in another

Calling the files in your code $file2 and $file1 doesn't help us to see which one is supposed to be "data" and which is supposed to be "search". Is this the sort of thing you are after:

#!/usr/bin/env perl use strict; use warnings; my $data = <<EOT; hostname banner exec EOT my $search = <<EOT; hostname XXXXX banner exec ********************************************************** +******************** banner exec</> banner exec This device belongs to the EOT for my $substr (split /\n/, $data) { for my $line (split /\n/, $search) { print "Matched '$substr' in '$line'\n" if index ($line, $subst +r) > -1; } }

Replies are listed 'Best First'.
Re^2: searching string from one file in another
by Eily (Monsignor) on Feb 28, 2017 at 13:35 UTC

    ++ on the bad variable names. The fact that the variables have numbers is a first sign that the names are probably not well chosen, but having two variables with the same names (a hash and a scalar named file2) should absolutely be avoided.

    Your proposition works fine if the lines in $data are unique, (the fact that they are counted using $file2{$line}++ makes me think they may not be) but will print duplicates otherwise.