Link to home
Start Free TrialLog in
Avatar of David Aldridge
David AldridgeFlag for United States of America

asked on

Parsing a string

Here is an example of a string I am trying to match:

SidDbData:A01|1|000588|01|01|S SID|A01|||164.103.137.43|3690|164.103.137.46|1098981768 APS|sapa01ci_A01_90|UNIX|SunOS|3290|3390 APS|sapapp25_A01_02|UNIX|SunOS|3202|3302 CYC|20041101|0014|1099320171|083744|1099319864 ABERR|0|0 ARFC|0|0 BDC|0|0 CCMS_ALERTS|0|0 ENQUEU|1|1 0014|083240|083744|0|85|80| ER_CON|0|0 IDOC_N|0|0 JOB|0|0 LOCKS|0|0 OLD_PROD|4|4 WA|DB-Maintain for Orac|0|4.3.00| GR|S.L. Reporter|0|4.3.00| MA|Manager|0|4.3.00| TR|Trak|0|4.3.00| PRINT|0|0 QRFC|0|0 SPOOL|0|0 SPOOL_SIZE|1|1 2915| TR_REQ|0|0 SID_PORTS|3|3 Msgs|sapa01ci|sapmsA01| Msgs_Http|sapa01ci|8190| Msgs_Https|sapa01ci||

I am picking out the "S SID" and I want to look for ER_CON| and the digit after it, IDOC_N| and the digit after it and JOB| and the digit after it. This is what I have so far and it works with the above trap:

my($sid, @test) = $string =~ /.* SID\|(.*?)\|.* ER_CON\|(\d?)\|.* IDOC_N\|(\d?)\|.* JOB\|(\d?)\|.*/;

print "\nSID equals $sid\n";
print "\nER_CON errors = ", $test[0], "\n";
print "IDOC_N errors = ", $test[1], "\n";
print "JOB errors = ", $test[2], "\n";

The problem is that SOMETIMES, either ER_CON, IDOC_N or JOB will either be in a different order or (most likely) not exist at all. If that happens, I still want to print out the one that DOES exist and it's value. Is there any way to do that? Also, I would appreciate any comments / suggestions one the way I am matching what I have so far.

Thanks!
holein5

Avatar of David Aldridge
David Aldridge
Flag of United States of America image

ASKER

I made a SLIGHT mistake in my regex.. here's what I have:

my($sid, @test) = $string =~ /.*S SID\|(.*?)\|.* ER_CON\|(\d?)\|.* IDOC_N\|(\d?)\|.* JOB\|(\d?)\|.*/
Avatar of manav_mathur
manav_mathur

What you can do is extract each of them seperately

if ($string =~ /.*S SID\|(.*?)\|/) {
print "$1" ;
}
else {
print "S SIDNot found" ;
}

and so on and so forth for each of your ER_CON, IDOC_N and JOB fields.

This will take care of any spurious order in which they may be occuring in your string and will aslo take care if they are not present entirely in the string

Manav
Try this,

#!/usr/local/bin/perl
use strict;
use warnings;
my $string="SidDbData:A01|1|000588|01|01|S SID|A01|164.103.137.43|3690|164.103.137.46|1098981768 APS|sapa01ci_A01_90|UNIX|SunOS|3290|3390 APS|sapapp25_A01_02|UNIX|SunOS|3202|3302 CYC|20041101|0014|1099320171|083744|1099319864 ABERR|0|0 ARFC|0|0 BDC|0|0 CCMS_ALERTS|0|0 ENQUEU|1|1 0014|083240|083744|0|85|80| ER_CON|0|0 IDOC_N|0|0 JOB|0|0 LOCKS|0|0 OLD_PROD|4|4 WA|DB-Maintain for Orac|0|4.3.00| GR|S.L. Reporter|0|4.3.00| MA|Manager|0|4.3.00| TR|Trak|0|4.3.00| PRINT|0|0 QRFC|0|0 SPOOL|0|0 SPOOL_SIZE|1|1 2915| TR_REQ|0|0 SID_PORTS|3|3 Msgs|sapa01ci|sapmsA01| Msgs_Http|sapa01ci|8190| Msgs_Https|sapa01ci||" ;
if ($string =~ /.*S SID\|(.*?)\|/) {
print "S SID : $1\n" ;
}
else {
print "S SID : Not found\n" ;
}
if ($string =~ /ER_CON\|(\d?)\|/) {
print "ER_CON : $1\n" ;
}
else {
print "ER_CON : Not found\n" ;
}
if ($string =~ /IDOC_N\|(\d?)\|/) {
print "IDOC_N : $1\n" ;
}
else {
print "IDOC_N : Not found\n" ;
}
if ($string =~ /JOB\|(\d?)\|/) {
print "JOB : $1\n" ;
}
else {
print "JOB : Not found\n" ;
}


Cheers
Manav
Ok, so does this look like the best way? It works, but it just seems inefficient.

#!/perl

use strict;
use warnings;

my $string = shift;
if($string =~ /.*S SID\|(.*?)\|.*/) {
    print "\nSID equals $1\n";
}
if($string =~ /.*ER_CON\|(\d?)\|.*/) {
    print "ER_CON errors = $1\n";
}
if($string =~ /.*IDOC_N\|(\d?)\|.*/) {
    print "IDOC_N errors = $1\n";
}
if($string =~ /.*JOB\|(\d?)\|.*/) {
    print "JOB errors = $1\n";
}
I don't need an error message if it's not found.. sorry about that.
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, thank you very much for the help! Looks like this is the one I'll be using.

holein5