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.

      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";'
      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.