Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Help with script to search all files for certain words.

by theninja (Initiate)
on Jan 10, 2011 at 21:24 UTC ( #881542=perlquestion: print w/ replies, xml ) Need Help??
theninja has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am having a hellofa time converting my shell script to a perl script. I am hoping to find some direction. The shell script basically searchs the system for all files and checks them for certain words, then pipes the output to a file, then strips out "fale-positive" results, and sends results to a file, then emails and ftp's file. On the perl side what I am having trouble with is the "grep"ing of the words out of the files. In the shell script I do this by

while read words.dat do find . -type f | xargs grep -i "${words.dat}=" 2>/dev/null >> out.log done < words.dat
this gives me the list of files that contain the word(s) in words.dat. I can get perl to work if I use each word individualy like this...
@find = `find . -type f | xargs grep -i sales= 2>/dev/null`; @find = `find . -type f | xargs grep -i price= 2>/dev/null`; @find = `find . -type f | xargs grep -i invoice= 2>/dev/null`;
but I need to feed it a list of words in a file. How can I do this? thanks

Comment on Help with script to search all files for certain words.
Select or Download Code
Re: Help with script to search all files for certain words.
by mikeraz (Friar) on Jan 10, 2011 at 21:36 UTC

    What have you tried?

    Perl's search operator can do or operations.

    while(<INFILE>) { if(/(sales|price|invoice)=/) { # do stuff } }


    Be Appropriate && Follow Your Curiosity
Re: Help with script to search all files for certain words.
by roboticus (Canon) on Jan 10, 2011 at 21:39 UTC

    Most greps have an option to give a filename containing a list of strings to search for, so use that. Since you're using words rather than patterns, use the fixed strings option as well:

    find blah blah blah | xargs grep -i --fixed-strings --file=words.dat

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      find blah blah blah | xargs grep -i --fixed-strings --file=words.dat

      Change that to ...

      find blah blah blah -print0 | xargs -0 grep -i --fixed-strings --file=words.dat

      ... and problems with "funny" filenames are gone.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Help with script to search all files for certain words.
by philipbailey (Chaplain) on Jan 10, 2011 at 22:46 UTC

    Something like this?

    #!/usr/bin/perl use strict; use warnings; use File::Find; my $re = get_re(); my @dirs = qw/./; find(\&wanted, @dirs); sub wanted { return unless -f $_; open my $fh, "<", $_ or warn "Couldn't open $_: $!\n"; while (<$fh>) { print "$File::Find::name:$_" if /$re/; } } sub get_re { my @words; while(<DATA>) { chomp; push @words, $_; } return join '|', map { "$_=" } @words; } __DATA__ sales price invoice
Re: Help with script to search all files for certain words.
by eff_i_g (Curate) on Jan 10, 2011 at 23:39 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2014-10-23 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls