Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Grep Pattern Match

by LostS (Friar)
on Dec 27, 2002 at 14:57 UTC ( [id://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";'

Replies are listed 'Best First'.
Re: Grep Pattern Match
by BrowserUk (Patriarch) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-24 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found