in reply to searching string from one file in another

Your post formatting is a bit off (and there's that trailing "This device belongs to the") but I've seen it updated once since I first opened it, so I suppose you are working on it. If you are having difficulties, you can look at Markup in the Monastery, or ask (maybe in the Chatterbox).

Anyway, the simplest way to check that a string is included in another is to use the index function (it will return the position of the match when there is one, or -1 when the substring can't be found). This would be something like:

my %file2; open my $file2, '<', shift or die; while ( my $line = <$file2> ) { chomp($line); # Do not keep the "\n" at the end of $line ++$file2{$line}; } open my $file1, '<', shift or die; while ( my $line = <$file1> ) { SEARCH: for my $search (keys %file2) { print $line and last SEARCH if index $line, $search > -1; # Ed +it: GotToBTru pointed out I forgot "index" } }
The for loop can be written with grep instead: print $line if grep { index $line, $_ > -1 } keys %file2;. You should use whatever is easier for you to understand and edit

++ to hippo on the variable names (if you have to add a number to a variable name, it's probably not the right name).

Edit2: s/a bit of\K/f/; Thanks to Lotus1.

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

    I think the intention clearer if you use 'any' (from List::MoreUtils) rather than 'grep'. A secondary benefit is that it is probably slightly faster because it can quit as soon as it finds one match.

    Update: Corrected typo and added link.

    Bill
Re^2: searching string from one file in another
by stray_tachyon (Initiate) on Aug 17, 2018 at 14:43 UTC
    Hi. What should the code be if I am trying to do a regex match (substring match) (i.e. look for "public" from file2 in "public sector union" in one of the lines in file1? Thanks a lot

      Hello stray_tachyon. Please post your question in a new post with the following information:

      • What your input looks like (a short example)
      • What your expected output looks like (from the example input)
      • What you have tried so far
      • How that failed and where you are stuck
      You can also read How do I post a question effectively?