How do I make it work for patterns such as "a*sample*.cgi" (the string to match is within the quotes)?

First of all, are you sure that the regular expression you give in your example is what you want to get? (0 or more "a"s followed by "sampl" and 0 or more "e"s, then, any character followed by "cgi"

Although I agree with the previous suggestions of using the glob function instead of your own grep, your script should work as well.

I guess that your problem is due to a premature expansion of your regular expression: if you don't enclose the regexp in quotes it will be expanded by the shell before passing the arguments to the script:

Suppose that you have a directory like this:

$ ls -1 /tmp/test/ other.txt test10.txt test1.txt test2.txt test3.txt

If you name your script "find_files.pl" and you call it with:

$ perl find_files.pl test*.txt

then, the shell will try to expand test*.txt and will pass the result of this expansion to the script. If you don't want this, you must put single quotes around it and provide the script with a valid perl regular expression like in:

$ perl find_files.pl 'test.*\.txt' wildcard is test.*\.txt /tmp/test/test10.txt /tmp/test/test3.txt /tmp/test/test2.txt /tmp/test/test1.txt

citromatik


In reply to Re: Wildcard search in a directory by citromatik
in thread Wildcard search in a directory by Anonymous Monk

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.