symgryph has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w #this file is for grepping within vdx files, and outputs the following +: #filename:<#of hits>:<arguments> #filename:2:myargument open ARGUMENTS, "<","file"; @files = <*.vdx>; @arguments = <ARGUMENTS>; foreach $file (@files) { foreach $argument (@arguments) { chomp $argument; `grep -c -H $argument \"$file\"`; chomp $results; print "$results:$arguments\n"; } }
I am writing the above code whoose purpose is to recursively go through a list of files, with a list of parameters to search for, and then print things that match, as well as their matching string.
The problem is that many of the filenames contain spaces etc. that grep hates and interprets as input. I figured out that grep can use quotes in order to 'escape' the spaces when searching through files, and using shell this works fine, but when execcing via the backtick operator, nothing happens, as if grep isn't being sent any filename.
Below follows with the two files, arguments, (aka file in the program), and the 'glob' pattern that matches all things it the current directory named 'vxd'. The script is then supposed to execute grep with quotes in the filename and print something like: filename:6:things_that_matched The '-H and the -c" in grep cause grep to print the count of things that matched, and -H prints the filename. I couldn't figure out how to get grep to print out the original argument that it was searching for.
hello goodbye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl backticks and GREP?
by moritz (Cardinal) on Oct 19, 2009 at 16:36 UTC | |
|
Re: Perl backticks and GREP?
by toolic (Bishop) on Oct 19, 2009 at 16:38 UTC | |
|
Re: Perl backticks and GREP?
by jakobi (Pilgrim) on Oct 19, 2009 at 17:12 UTC | |
by symgryph (Sexton) on Oct 19, 2009 at 18:06 UTC | |
by johngg (Canon) on Oct 19, 2009 at 19:46 UTC | |
by jakobi (Pilgrim) on Oct 19, 2009 at 20:22 UTC | |
|
Re: Perl backticks and GREP?
by leocharre (Priest) on Oct 19, 2009 at 16:38 UTC |