1

I have a directory containing 'x' no. of files. Some files contain a "Pass" string and some others with "Fail". I want to read both files and store the pass and fail files separately into two different directories. I am able to read the files but can not match the string.

#!/usr/bin/perl

use strict;
use warnings;

use Path::Class;

my $src = dir("/home/Narayanan/Cov_script/logs/");
my $pattern = "TEST IS PASSED";
my $i = 0;
my @log_files = grep /[.] log\z/x, $src->children;

for my $log_file ( @log_files ) {
    my $in = $log_file->openr;
    while (my $line = <$in>) {
    my $string = $line; 
#| `grep  { $line eq $pattern } `

        print "OUT: $string";
    }
}
nanofarad
  • 20,717
nani
  • 25
  • 5

1 Answers1

1

There is a =~ operator for matching a string to a pattern. This seems to work for me (I think it should be foreach instead of for, but for and foreach are synonyms, so you could use either):

foreach my $log_file ( @log_files ) {
    my $in = $log_file->openr;
    while (my $line = <$in>) {
       my $string = $line; 
       if ($line =~ /$pattern/)
       {
         #| `grep  { $line eq $pattern } `
           print "OUT: $string";
       }
    }
}
David Dyck
  • 78
  • 7
Scooter
  • 702