Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Grep Pattern Match

by LostS (Friar)
on Dec 27, 2002 at 14:57 UTC ( #222544=perlquestion: print w/ replies, xml ) Need Help??
LostS has asked for the wisdom of the Perl Monks concerning the following question:

OK I suck at trying to figure this pattern match stuff out. Here is the situation. I have an array of all the files in a directory. I need to grep this array and pull out all the files that match this pattern. nc, 2 digit year and ends in .log. So it would find a file that looks like this nc021225.log or nc0201.log. Just need to figure out how to do that pattern match or be giving a link to figure it out.

-----------------------
Billy S.
Slinar Hardtail - Hand of Dane
Datal Ephialtes - Guildless
RallosZek.Net Admin/WebMaster

perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Comment on Grep Pattern Match
Download Code
Re: Grep Pattern Match
by BrowserUk (Pope) on Dec 27, 2002 at 15:01 UTC

    Try this. It could be made more specific if the rest of the characters are always digits, but I've stuck to your written description of requirements.

    @matched = grep /^nc\d\d.*\.log$/, @files

    Examine what is said, not who speaks.

      Would this syntax be correct:
      @files = grep {/^nc$year.*\.log$/} @files;
      or
      @files = grep {/^nc.$year.*\.log$/} @files;
      Where the $year is the 2 digit number of the year I am in??

      -----------------------
      Billy S.
      Slinar Hardtail - Hand of Dane
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

        The former looks right to me, though the easiest way is to check is to try it:)

        Why did you think that you might need the extra '.' in the latter?


        Examine what is said, not who speaks.

      Thank you... Your nudge got me in the right direction. here is the code I am using: @files = grep {/^nc$digyear$digmon.*\.log$/} @files; Works like a charm :)

      -----------------------
      Billy S.
      Slinar Hardtail - Hand of Dane
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-20 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls