×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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

CODE --> perl

Error: Generated clock * 

CODE --> 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:

CODE --> 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";
    }
} 

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

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 && index($error_msg, $err) != -1) {
        push @{$grouped_errors{$owner}{$justification}}, {
          error => $error_msg,
          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 

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

Is this what you needed or am I completely wrong ?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close