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

Hi all,
I seek your help in solving probably a silly problem.
I do a grep on my root and I need to parse out results as:
$FileName, $Path.
I used  $grepResult=~/(\w+[\.\w+]+)(\:)/; and took $1,$` for file and path respectively.
Now I face problem when certain file names are encountered like:
/tools/sav/GlobalPS.pl.2005-04-14
/tools/TitleLiner.cgi-ELFE-fixed etc.
$term=<some text input to be searched for>; $fn="../ -depth -type f | xargs grep -i -n "."$term"; $output = qx/find $fn /;
$output is written to a file to be later read and parsed by perl.
I used find to look recursively as I work on Solaris
It would be great if someone to tell me how to overcome
slowness of 'find | xargs grep' (or any other recurssive grep) on Solaris.

Thanking all in advance!

Replies are listed 'Best First'.
Re: Parse grep result
by loris (Hermit) on Jan 30, 2008 at 11:33 UTC

    It is not entirely clear to me what your problem is. If you are trying find arbitrary files within the file system, then you could just call

    find / -name "*<search term>*

    in your program (the find|xargs grep idiom is usually used when your grep does not have an option for recursion, as may well be the case on Solaris). This however will take a while if you have a large file system. If you have the locate command, use this - it will be much quicker.

    BTW, if you do want to parse paths, use File::Basename.

    loris


    "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)
      Sorry for being brief...
      I was looking to list contents(files) of my folders including subdirectories which have the search term in them.
      Another query will File::Basename. more quick than 'find | xargs grep, ?

        File::Basename is just for parsing paths, not for searching for files in the file system.

        loris


        "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)
Re: Parse grep result
by cdarke (Prior) on Jan 30, 2008 at 13:45 UTC
    See File::Find. Perl really is worth using, rather than programs like grep(1), find(1), and xargs(1).
Re: Parse grep result
by hipowls (Curate) on Jan 30, 2008 at 11:36 UTC

    Use File::Basename, it is platform independent

    use File::Basename qw(basename dirname); my $path = '/usr/local/bin/perl'; my $file = basename $path; # $file eq 'perl' my $dir = dirname $path; # $dir eq '/usr/local/bin'

    If you really must do it yourself then

    my ($dir, $file) = $path =~ m{^(?:(.*)/)?([^/]+)$}x;
    See why I recommend File::Basename?