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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl file parsing and extracting certain info

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi list - how would I do following.

I've got a file with over 500 blocks like following. I need to parse the file consisting of blocks starting with CLASS end Ending with END. Inside those block is the information I need to extract. For all lines lines inside each block starting with 1: to 11: I need to extract the values inside the " " and put it in one line into a new file.
I did it in a shell script and the script ran for 4,5 hours. So I like to rewrite in perl.


CLASS IM_15004
SELECT
1: ATTR(=,$ENTERPRISE) , VALUE PREFIX, "1.3.6.1.4.1.232" ) ;
2: $SPECIFIC = 15004;
3: ATTR(=, "sysName") ;
4: ATTR(=, "cpqClusterNodeName");
MAP
sub_source = HP_Insight_Manager;
specificTrap = $V2;
severity = FATAL;
hostname=$V3;
hpq_ClusterNodeName = $V4;
hpq_eventID = $V2;
msg = "Cluster Node Failed";
hpq_info = PRINTF ( "Cluster service on %s has failed.", $V4);
END

CLASS IM_15005
SELECT
1: ATTR(=,$ENTERPRISE) , VALUE(PREFIX, "1.3.6.1.4.1.232" ) ;
2: $SPECIFIC = 15005;
3: ATTR(=, "sysName") ;
4: ATTR(=, "cpqClusterResourceName");
MAP
sub_source = HP_Insight_Manager;
specificTrap = $V2;
severity = CRITICAL;
hostname=$V3;
hpq_ClusterResourceName = $V4;
hpq_eventID = $V2;
msg = "Cluster Resource Degraded";
hpq_info = PRINTF ( "Cluster resource %s has become degraded.", $V4);
END
 
?

Code:
[b]#!/usr/bin/perl[/b]

while (<DATA>) {
  if (/^CLASS (.*)$/) {
    print "\n" if ++$counter > 0;
    print "$1\n";
  } elsif (/\b\d+: .* "([^"]+)"/) {
    chomp ($lookup = `grep $1 trap.oid`);
    $lookup =~ s/^"[^"]+" //;
    print "$lookup ";
  }
}

print "\n";

__DATA__
CLASS IM_15004
  SELECT
    1: ATTR(=,$ENTERPRISE) , VALUE PREFIX, "1.3.6.1.4.1.232" ) ;
    2: $SPECIFIC = 15004;
    3: ATTR(=, "sysName") ;
    4: ATTR(=, "cpqClusterNodeName");
  MAP
    sub_source = HP_Insight_Manager;
    specificTrap = $V2;
    severity = FATAL;
    hostname=$V3;
    hpq_ClusterNodeName = $V4;
    hpq_eventID = $V2;
    msg = "Cluster Node Failed";
    hpq_info = PRINTF ( "Cluster service on %s has failed.", $V4);
END

CLASS IM_15005
  SELECT
    1: ATTR(=,$ENTERPRISE) , VALUE(PREFIX, "1.3.6.1.4.1.232" ) ;
    2: $SPECIFIC = 15005;
    3: ATTR(=, "sysName") ;
    4: ATTR(=, "cpqClusterResourceName");
  MAP
    sub_source = HP_Insight_Manager;
    specificTrap = $V2;
    severity = CRITICAL;
    hostname=$V3;
    hpq_ClusterResourceName = $V4;
    hpq_eventID = $V2;
    msg = "Cluster Resource Degraded";
    hpq_info = PRINTF ( "Cluster resource %s has become degraded.", $V4);
END


Kind Regards
Duncan
 
Sorry,
This might be better:

Code:
#!/usr/bin/perl -w
use strict;
my %hash = ();
open FH, "trap.oid" or die "Failed to open trap.oid";
while(<FH>) {
  /"([^"]+)"\s+"([^"]+)"/
    and $hash{$1} = $2;
}
close FH;
open FH, "trap.cds" or die "Failed to open trap.cds";
my @strings;
while(<FH>) {
  chomp;
  @strings = (), print "$1\n" if(/^CLASS (.*)$/);
  if(/^\s+(\d+):[^"]+"([^"]+)"/) {
    if(exists $hash{$2}) {
      push @strings, $hash{$2};
    } else {
      push @strings, $2;
    }
  }
  push @strings, $1 if(/^\s+\d+:\s+\$SPECIFIC\s+=\s+(\d+);/);
  print join(" ", @strings), "\n" if(/^END/);
}
close FH;


Trojan.
 
Perfect solution - comparing to my shell script it's million times faster. I appretiate your help a lot. Guess I owe you a drink. Thanks a lot, Dietmar
 
didn't make much of an impact there did i :-(


Kind Regards
Duncan
 
Don't worry duncdude,
You normally make quite an impact.
Makes a nice change for someone else to be "the one".
;-)


Trojan.
 
Kind comments again from the PerlMaster :)


Kind Regards
Duncan
 
Oh no - PLEASE DON'T DO THAT!

I can see it now... two people in the forum going nuts trying to solve hugely complex problems like prime number theory & thermodynamics - with single line scripts!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top