Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl how to read string in csv file * as wildcard operator

Status
Not open for further replies.

newbie1238585

Programmer
Aug 2, 2023
1
MY
I am comparing a csv file with a text file and match the error when regex pattern matched. I pasted whole script to ease the help. How to recognize asterisk * as wildcard operator when reading csv file? "Parse csv file part" in the code below should make the change.

Expected output when csv file has * wildcard
Perl:
Error: Generated clock *

Perl:
WAIVED (3 waivers) - setup_timing,100,blockA:
    Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
    Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
    Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)
    Waiver Owner: alex
    Ticket no: testing
    Justification: NA

My csv:

Code:
#Process,#Block,#Stage name,#Waiver type,#Waiver owner,#Justification,#Ticket number,#Error msg
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock *

Error file:

Code:
Error:
Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)

Script:

Perl:
#!/usr/bin/perl
use Text::CSV;
use strict;
use warnings;


my $csv = Text::CSV->new({ sep_char => ',' });
my $collect;

# Open and read the CSV file
open(my $data, '<', "waiver2.csv") or die "Could not open 'file.csv' $!\n";

# Open and read the error file
open(my $err, '<', "file2.txt") or die "Could not open 'error.txt' $!\n";

my $stage_name = '';
my $process_name = '';
my $block_name = '';
my @errors;
my %unwaived_errors;
my @unwaived_order;

# Parse the file
while (my $line = <$err>) {
    chomp $line;
    # Start collecting when "Error:" is found
    if ($line eq 'Error:') {
        $collect = 1;
        next;
    }

    # Stop collecting when an empty line is found
    if ($collect && $line eq '') {
        $collect = 0;
    }

    # Collect lines
    if ($collect) {            
        push @errors, $line;
        if (!exists $unwaived_errors{$line}) {
            push @unwaived_order, $line;
        }
        $unwaived_errors{$line}++;
    }
}

close $err;

my %grouped_errors;

# Parse the csv file
while (my $line = <$data>) {
    chomp $line;

    if ($csv->parse($line)) {
        my @fields = $csv->fields();

        # check for error messages only in the rows with actual data (not header)
  if ($fields[0] =~ /^\d/) {
    my ($process, $block, $stage, $waiver_type,$owner, $ticket, $justification, $error_msg) = @fields[0, 1, 2, 3, 4, 5, 6,7];

    # check for each error in the error.txt file
    foreach my $err (@errors) {
      if ($stage_name eq $stage && $process_name eq $process && $block_name eq $block && index($err, $error_msg) != -1) {
        push @{$grouped_errors{$owner}{$justification}}, {
          error => $err,
          stage => $stage,
          process => $process,
          block => $block,
          ticket => $ticket
        };
        $unwaived_errors{$err} = 0 if exists $unwaived_errors{$err}; # Here we set the counter to 0 for a waived error
        last;
                }
            }
        }
    } else {
        warn "Line could not be parsed: $line\n";
    }
}

close $data;

my $number_errors_waived = 0;

# Print out the grouped errors
foreach my $owner (keys %grouped_errors) {
    foreach my $justification (keys %{$grouped_errors{$owner}}) {
        my $first_error_data = $grouped_errors{$owner}{$justification}[0];
        $number_errors_waived += @{$grouped_errors{$owner}{$justification}};
        print "\nWAIVED ($number_errors_waived waivers) - $first_error_data->{stage},$first_error_data->{process},$first_error_data->{block}:\n";
        foreach my $error_data (@{$grouped_errors{$owner}{$justification}}) {
            print "    $error_data->{error}\n";
        }
        print "    Waiver Owner: $owner\n";
        print "    Ticket no: $first_error_data->{ticket}\n";
        print "    Justification: $justification\n";
    }
}

# Print out the unwaived errors
my @unwaived_errors;
foreach my $error (@unwaived_order) {
    if ($unwaived_errors{$error} > 0) {
        push @unwaived_errors, $error;
    }
}
my $number_unwaived_errors = @unwaived_errors;
if (@unwaived_errors) {
    print "\nFAILED (0 waiver, $number_unwaived_errors unwaived) - $stage_name,$process_name,$block_name:\n";
    foreach my $unwaived_error (@unwaived_errors) {
        print "    $unwaived_error\n";
    }
}
 
Hi newbie1238585,

I'm not sure if I undestand what you exactly need ...

But, if you don't want to have in your Error file all messages literally like now
Code:
Error:
Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)

but only patterns like this
Code:
Error:
Error: Generated clock

then you could make changes in this code
Code:
      if ($stage_name eq $stage && $process_name eq $process && $block_name eq $block && index($err, $error_msg) != -1) {
        push @{$grouped_errors{$owner}{$justification}}, {
          error => $err,
          stage => $stage,
          process => $process,
          block => $block,
          ticket => $ticket
        };

like this
Code:
      if ($stage_name eq $stage && $process_name eq $process && $block_name eq $block && [highlight #FCE94F]index($error_msg, $err) != -1[/highlight]) {
        push @{$grouped_errors{$owner}{$justification}}, {
          [highlight #FCE94F]error => $error_msg[/highlight],
          stage => $stage,
          process => $process,
          block => $block,
          ticket => $ticket
        };

and you will get with this CSV file
Code:
#Process,#Block,#Stage name,#Waiver type,#Waiver owner,#Justification,#Ticket number,#Error msg
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
100,blockA,setup_timing,T,alex,testing,NA,Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)

this output
Code:
WAIVED (3 waivers) - setup_timing,100,blockA:
    Error: Generated clock 123abc is not satisfiable; zero source latency will be used. (GRF-011)
    Error: Generated clock 678trt is not satisfiable; zero source latency will be used. (GRF-011)
    Error: Generated clock 789def is not satisfiable; zero source latency will be used. (GRF-011)
    Waiver Owner: alex
    Ticket no: testing
    Justification: NA
 
Is this what you needed or am I completely wrong ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top