Search for string and return file name

Discussion in 'Perl Misc' started by Örjan Johansson, Nov 17, 2004.

  1. Hi all!

    I'm trying to figure out how to go through all files with a certain
    extension in a directory, search for a string and return the names of the
    files that contains the string. I have written scripts before that opens a
    single file for reading and writing, but have no clue how to go through all
    files in a folder, like a wildcard *.log kind of thing. Any pointers?

    TIA,
    Örjan
     
    Örjan Johansson, Nov 17, 2004
    #1
    1. Advertising

  2. Jim Gibson wrote:
    > In article <9Jwmd.122063$>, Örjan Johansson
    > <> wrote:
    >
    >
    >>Hi all!
    >>
    >>I'm trying to figure out how to go through all files with a certain
    >>extension in a directory, search for a string and return the names of the
    >>files that contains the string. I have written scripts before that opens a
    >>single file for reading and writing, but have no clue how to go through all
    >>files in a folder, like a wildcard *.log kind of thing. Any pointers?

    >
    >
    > See 'perldoc -f glob' or check out the Find::File module.

    Might it be File::Find instead of Find::File?
    >
    > Simply put, you can use a loop of the type
    >
    > for( <*.ext> ) {
    > print "file is $_\n";
    > }
    >
    > to iterate over all of the files with extension 'ext' in the current
    > directory.
     
    MrReallyVeryNice, Nov 17, 2004
    #2
    1. Advertising

  3. Örjan Johansson

    Gerhard M Guest

    "Örjan Johansson" <> wrote in message news:<9Jwmd.122063$>...
    > Hi all!
    >
    > I'm trying to figure out how to go through all files with a certain
    > extension in a directory, search for a string and return the names of the
    > files that contains the string. I have written scripts before that opens a
    > single file for reading and writing, but have no clue how to go through all
    > files in a folder, like a wildcard *.log kind of thing. Any pointers?
    >
    > TIA,
    > Örjan


    if you're using ...IX try
    grep -l "text" *.log

    using perl try glob
    foreach $file (glob "*.log") {
    open FH,$file;
    while (<FH>) {
    if (m/text/) {print $file; last; }
    }
    close FH;
    }
     
    Gerhard M, Nov 17, 2004
    #3
  4. On Wed, 17 Nov 2004 00:13:57 GMT, "Örjan Johansson"
    <> wrote:

    >Hi all!
    >
    >I'm trying to figure out how to go through all files with a certain
    >extension in a directory, search for a string and return the names of the
    >files that contains the string. I have written scripts before that opens a
    >single file for reading and writing, but have no clue how to go through all
    >files in a folder, like a wildcard *.log kind of thing. Any pointers?


    It's not clear if you want to search directories recursively or not.
    In the latter case I'd simply let the shell do wildcard expansion, a
    la

    perl findfiles.pl mydir/*.log

    with findiles.pl along these lines:

    #!/usr/bin/perl -ln

    use strict;
    use warnings;

    print $ARGV and close ARGV
    if /whattaiwant/;

    __END__


    If your shell doesn't do wildcard expansion you may want to do it
    yourself inserting this line:

    BEGIN {@ARGV=map glob, @ARGV}

    If you want to search recursively then File::Find or one of the
    modules that build upon it is your way.


    HTH,
    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Nov 17, 2004
    #4
  5. Örjan Johansson

    Tore Aursand Guest

    On Wed, 17 Nov 2004 02:17:26 -0800, Gerhard M wrote:
    > if you're using ...IX try
    > grep -l "text" *.log


    What if you're using Linux? :)


    --
    Tore Aursand <>
    "Out of missiles. Out of bullets. Down to harsh language." (Unknown)
     
    Tore Aursand, Nov 17, 2004
    #5
  6. Örjan Johansson wrote:
    >
    > I'm trying to figure out how to go through all files with a certain
    > extension in a directory, search for a string and return the names of the
    > files that contains the string. I have written scripts before that opens a
    > single file for reading and writing, but have no clue how to go through all
    > files in a folder, like a wildcard *.log kind of thing. Any pointers?


    perl -lne'/string/ and print $ARGV and close ARGV' *.log


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 19, 2004
    #6
  7. Örjan Johansson wrote:
    > Hi all!
    >
    > I'm trying to figure out how to go through all files with a certain
    > extension in a directory, search for a string and return the names of the
    > files that contains the string. I have written scripts before that opens a
    > single file for reading and writing, but have no clue how to go through all
    > files in a folder, like a wildcard *.log kind of thing. Any pointers?
    >
    > TIA,
    > Örjan
    >
    >

    Thanx all you guys. Just like I suspected, there are tons of ways to
    achieve everything. I've got several ideas to work with now. Thanks
    again everyone for your input!

    Sincerely,
    Örjan
     
    Örjan Johansson, Nov 20, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    884
    Keith Thompson
    Mar 6, 2005
  2. Mike Schilling
    Replies:
    2
    Views:
    600
    Arne Vajhøj
    Mar 5, 2010
  3. Abby Lee
    Replies:
    5
    Views:
    480
    Abby Lee
    Aug 2, 2004
  4. Replies:
    1
    Views:
    207
    Dr.Ruud
    Jul 29, 2006
  5. My Python
    Replies:
    0
    Views:
    647
    My Python
    Apr 17, 2012
Loading...

Share This Page