Regular expression for capturing values - Perl

-1

I am writing a parser that will capture the value from text file. Apart from two values I am able to capture all values.

Can some one help me out to get those two values as well?

Here is my code:

use warnings;

open( my $fh, "<", "$tZPath\\Diag.txt" ) || print "can't open file filename : $!\n";

my ( $aBT_Slave, $iD, $hready, $address );

while ( defined( my $line = <$fh> ) ) {

    if ( $line =~ /ID\:\s*(.+?\W+)/i ) {
        $iD = $1;
        print " Id -> $iD \n";
    }
    elsif ( $line =~ /HREADY\:\s*(.+?\W+)/i ) {
        $hready = $1;
        print " hready -> $hready \n";
    }
    elsif ( $line =~ /ADDR0\w\s*(.+?\W+)/i ) {
        $address = $1;
        print " address -> $address \n";
    }
    elsif ( $line =~ /PCNOC\_(.*?\d+)/i ) {
        $aBT_Slave = "PCNOC_" . $1;
        print " aBT_Slave -> $aBT_Slave \n";
    }
}

Log file content:

ABT PCNOC_9 ID: 0x0000430c
ABT PCNOC_9 ADDR0: 0x000000e0
ABT PCNOC_9 ADDR1: 0x00000000
ABT PCNOC_9 HREADY: 0xfffffffd
ABT PCNOC_9 Slaves: 5
Fatal Error: AHB_TIMEOUT

Can anyone please help me out to get the ADDR0 value and PCNOC_9 from the log? Please let me know if have kept unclear expiation here.

perl
asked on Stack Overflow Apr 5, 2016 by user3688062 • edited Apr 5, 2016 by Borodin

3 Answers

1

Please try this, It will make fetching fully dynamic.

while (defined(my $line = <$fh>)) {
      if($line =~ /(PCNOC\_.*?\d+)\s*(\w+):\s*(.*)/i){
            print "$1 ==> $2 ==> $3\n";
      }
}
answered on Stack Overflow Apr 5, 2016 by mukesh bhoj
0

You can try this.

if($line =~ /ADDR1:\s*(.*)/i){
.....
}
elsif($line =~ /ADDR0:\s*(.*)/i){
.....
}
answered on Stack Overflow Apr 5, 2016 by mukesh bhoj
0
/ADDR0\w\s*(.+?\W+)/i
      ^^

This fails to match because : is not a word char. Fix:

/ADDR0:\s*(.*)/
answered on Stack Overflow Apr 5, 2016 by ikegami

User contributions licensed under CC BY-SA 3.0