pablo has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 files which contain loads of text. In both of the files there are lines which begin 'Query:' (these are embedded between other lines of text which I am not interested in). On the same line following the word 'Query:' is the text that I am interested in let's call it 'blurb'. What I need to do is find every occurrence of blurb which exists in file1 and file2. I would apreciate any advice on this matter as I am at a loose end, the only solution I can think of is: Search through file1 for the word Query:, store the text following this in a variable 'blurb', then search through file2 for the variable 'blurb'. Then return to File1 and repeat.

Replies are listed 'Best First'.
Re: Comparing strings in 2 files.
by Albannach (Monsignor) on Dec 21, 2000 at 20:25 UTC
    Since TIMTOWTDI, here's a slight variation on one of my favorite one-liners (original by merlyn): perl -ne 'print if ($seen{$_} .= @ARGV && /^Query:/) =~ /10$/' fileA fileB > output
Re: Comparing strings in 2 files.
by I0 (Priest) on Dec 21, 2000 at 19:40 UTC
    If I am understanding your question, you might do something likesomething like:
    open FILE "<file1.text" or die "can't open file1 $!"; close FILE; while( <FILE> ){ $blurb{$_}++ if /^Query\b/ } open FILE "<file2.text" or die "can't open file2 $!"; while( <FILE> ){ print if /^Query\b/ && $blurb{$_}; } close FILE;