Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

find or grep?

by nkpgmartin (Sexton)
on Jun 21, 2001 at 17:34 UTC ( [id://90348]=perlquestion: print w/replies, xml ) Need Help??

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

I have a file called blah created every day in a subdirectory named $day - 1, so for example, file for June 21 is made in Wednesday/blah. How can I get the following idea to work in my script:
$file = find ./ -name blah -exec ls {} \;
OR:
$file = grep -M blah, <*>;
I can't get find to work in my script, and grep doesn't print out the full path. My idea is to save the path (which changes every day) as a variable. I figure this is easier than having my script decide the day of week - 1. Is there a way to do this without installing any modules? Thanks! Happy summer solstice.

Replies are listed 'Best First'.
Re: find or grep?
by ZZamboni (Curate) on Jun 21, 2001 at 17:41 UTC
    The find line that you have won't work, unless you enclose the find command in backticks (and are on Unix). See the documentation for the File::Find module to see the proper way of doing a find from Perl. It's a standard module, so you wouldn't have to install anything.

    --ZZamboni

      If I use:
      use File::Find; find(\&wanted, '$file'); sub wanted { $rlfile = `find2perl / -name $file -exec ls {} \;`; }
      I get the error: cant stat blah: no such file or directory. How can I get $rlfile to equal the full path name to blah (which is $file)?
Re: find or grep?
by runrig (Abbot) on Jun 21, 2001 at 18:27 UTC
    You could use File::Find, which should already be installed:
    find(sub {print $File::Find::name if $_ eq 'blah'}, ".");
    Another option is file globbing:
    my $blah = <*/blah>; print $blah; # Or if more than one @blahs = <*/blah>; print "$_\n" for @blahs;
      Okay this works:
      use File::Find; $fullpath = `find $path/*/$file \;`;
      where * is the day. Thanks for your help.
        good to see you got something that works. but you're mixing your methods in this example. you're getting the return value you want in  $fullpath, but you're not using the File::Find module to do it. instead, you're using the backquotes, which executes an external program and returns the output as a single string containing all lines.
        so, you've gone to the trouble of loading all these lines of code, only to ignore them. perhaps you should remove the use line, it's unneccessary in your example.

        as an alternative, for an example of a 100% Pure Perl solution, try:

        #!/usr/bin/perl use strict; use warnings; use File::Find; # the dir you're 'find'ing from my $dir = shift || '.'; # the file you're looking for my $file = 'blah'; # the results you want my @results; # the function you perform on each find value sub looking_for_file { # push full filename to an array if file and name is $file push(@results, $File::Find::name) if(-f $_ && $_ eq $file); }; # the result you want, using File::Find::find() # (not the external 'find' program) find \&looking_for_file, $dir; # print your results print("my results are: @results\n");
        In this example, you get an array (@array) with the values of each file found named 'blah'. unlike also, this should run on just about any platform.

        I know my example was a bit lengthy, so here's a shorter version:

        #!/usr/bin/perl use strict; use warnings; use File::Find; # the shorter version my @results; find sub{-f $_ && $_ eq 'blah' && push(@results,$File::Find::name)}, shift || '.'; print("my results are: @results\n");

        ~Particle

Re: find or grep?
by fundflow (Chaplain) on Jun 21, 2001 at 17:40 UTC
    Did you try 'grep --with-filenames'?

    It works on gnu version (e.g. linux)

    I have this alias for recursive grep:

    alias Grep 'echo Recursive Grep:;find . -type f -exec fgrep --with-filename \!* \{\} \;'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://90348]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found