#!/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
"Two Wheels good, Four wheels bad."

In reply to Perl backticks and GREP? by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.