Professional Web Applications Themes

Finding a string in a file - PERL Beginners

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

0\webdata.tab. The last three folders change frequently so I can have c:\data\appl0\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...

  1. #1

    Default 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

    Debbie Guest

  2. #2

    Default Re: Finding a string in a file

    Debbie Cooper wrote on 25.05.2004:
     

    Sure. Make use of File::Find to traverse the directory structure. For each file, you will have the whole path in the $File::Find::name variable. You could do something like

    use File::Find;
    find(\&wanted, directories_to_search);

    sub wanted {
    if $File::Find::name =~ /webdata.tab/ {
    # open the file and look for the header
    # return a message including the $FIle::Find::name if the header isfound
    }
    }

    HTH,

    Jan
    --
    If all else fails read the instructions. - Donald Knuth
    Jan Guest

  3. #3

    Default Re: Finding a string in a file


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

     
    in 
    like 
    file 
    file 
    found. 

    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


    Chris Guest

  4. #4

    Default Re: Finding a string in a file

    Debbie Cooper wrote: 

    You could do something like this (untested):

    ARGV = glob 'C:/data/*/*/*/webdata.tab';

    my files;
    while ( <> ) {
    push files, $ARGV if /string/;
    close ARGV if $. == 5; # five header lines?
    }

    print "$_\n" for files;



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Finding in a string, and other items
    By Picture Dots in forum PHP Development
    Replies: 17
    Last Post: February 15th, 01:07 PM
  2. finding and extracting from a string
    By lecichy in forum PHP Development
    Replies: 4
    Last Post: October 11th, 05:44 AM
  3. Finding text within a string
    By Tim Williams in forum ASP
    Replies: 2
    Last Post: July 6th, 07:53 PM
  4. Finding two spaces in a string
    By J. Muenchbourg in forum ASP
    Replies: 1
    Last Post: July 1st, 08:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139