----- Original Message ----- 
From: "Debbie Cooper" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <[EMAIL PROTECTED]>
Sent: Tuesday, May 25, 2004 12:11 PM
Subject: Finding a string in a file


> I need to search for the occurrence of a string in a file that is buried
in
> directories.  So, for example, I have a directory structure that looks
like
> this C:\data\elec\1\220\webdata.tab.  The last three folders change
> frequently so I can have c:\data\appl\3\180\webdata.tab and so on.  The
file
> I'm searching will always be called webdata.tab.  It is a tab delimited
file
> with headers and I need to search the header for a specific word like
> "Brand" and somehow return the directory structure where the word is
found.
> Can this be done in "beginning" perl?
>
> Thanks,
> Debbie
>

Hi Debbie

The following code will do what you want, I think.

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;

my @directories = ("C:/data/");

find(\&wanted,  @directories);

sub wanted {
    if ($_ eq "webdata.tab") {
        open my $fh, "<", "$_" or die "open $_: $!";
        while (my $line = <$fh>) {
            if ($line =~ /\bBrand\b/) {
                print"$File::Find::dir\n";
                last;
            }
        }
        close $fh;
    }
}


HTH
Chris



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to