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

How do I search for a word pattern like "Var_x_longitude"
"Var_y_latitude" two files to see if such words are in these two files?

I was trying to open a file and do a (/\bVar*e\b) and then try to save it in an array. Then open the 2nd file and compare the array with the pattern. But I failed.

I found out here that there is a command like

perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' fileA fileB >output
but I dont' understand it and it only matches the length of the lines in two files.

I have a variable and want to see if they are used in these 2 files and want to see the display or the lines either on standard out or > output.txt. Can anyone help? I'm not good at programming. Using C seems even harder.

Any help would be appreciated much. Thanks.

DAVE

Edit 2001-03-16 by tye

Replies are listed 'Best First'.
Re: Find certain words in multiple files
by arturo (Vicar) on Mar 17, 2001 at 04:09 UTC

    if you don't want to do much programming: , you could try the following:

    #!/usr/bin/perl -w # literally, ALL this will do is show you whether the # string you're searching for is in both files. # you might want to know line numbers, too, but this won't # do it without modification. use strict; # get variable, and filenames from wherever ... if (in_file($pattern, $filename1) && in_file($pattern, $filename2) ) { print "Found $pattern in both $filename1 and $filename2\n"; } else { print "$pattern not found in both $filename1 and $filename2.\n"; } sub in_file { my ($pattern, $filename) = @_; open FILE, $filename or die "Can't open $filename: $!\n"; my $found =0; while (<FILE>) { # set found to a true value and exit the loop # if we find the pattern in the current line. $found=1, last if /$pattern/; } close FILE; $found; }

    Of course, if you're on a *nix system, if (`/usr/bin/grep $pattern $filename1` && `/usr/bin/print grep $pattern $filename2`) {} could replace that whole sub, but then you have to worry about users putting in dumb patterns.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Find certain words in multiple files
by buckaduck (Chaplain) on Mar 17, 2001 at 04:36 UTC
    Actually your pattern could work with a slight modification:
    /\bVar.*e\b/
    My only changes:
    • You want to use .* (match any character zero or more times) instead of r* (Match the letter 'r' zero or more times)
    • I added the ending slash (/); hopefully that was just a typo on your part.
    buckaduck