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

Regex push $1 help 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I have a rather large text file (about 1MB) that has 1000s of entries about 20 lines long a piece, plus alot of other junk in it. Each entry in the file I want begin with the word "DESCRIPTION" in caps. Such as:

junk
junk
junk
DESCRIPTION first entry stuff I want
stuff I want
stuff I want
DESCRIPTION next entry stuff I want
next stuff I want
next stuff I want
DESCRIPTION
etc
etc

What I want to do is take each entry individually, starting with the DESCRIPTION, and push that entry into an array, so at the end I have one array cell for each entry between the DESCRIPTIONS.


Here is my whack at the code so far:

my $filename = "templrep.log";
my $str;
my @templates;

open (FILE,"/tmp/templrep.log") or die ("Cannot open $filename: $1" );

my $arr = <FILE>;

push (@templates, $1) while( $arr =~ m/DESCRIPTION(.*?)DESCRIPTION/gi );
print &quot;@templates\n&quot;;

exit 0;


Obviopusly it is not working.

Any help is appriciated.

 
By default the input record separator is a new line. So each time you read from a file, it reads one line at a time. But since you want to read data between 'DESCRIPTION' strings, you should redefine the input record separator to DESCRIPTION like this:

my $filename = &quot;templrep.log&quot;;
my @templates;
my $InRecSep = $\;
$\ = 'DESCRIPTION';
open (FILE,&quot;/tmp/templrep.log&quot;) or die (&quot;Cannot open $filename: $1&quot; );
while(<FILE>) {
push(@templates, $_);
}
$\ = $InRecSep;
 
Thanks Raider, but it does not seem to be working. I decided to use 'Template' as my seperator because it works better for the file I am reading, but I am still not getting what I want. Here is my new code:

my $filename = &quot;templrep.log&quot;;
my @templates;
my $InRecSep = $\;
$\ = 'Template';
open (FILE,&quot;/tmp/templrep.log&quot;) or die (&quot;Cannot open $filename: $1&quot; );
while(<FILE>) {
push(@templates, $_);
print &quot;$_\n&quot;;
print &quot;------------------------------------------------------------------------------------------------------------------------------\n&quot;;
}
$\ = $InRecSep;


exit 0;




Here is a sample entry from the report I am reading:

# ----------------------------------------------------------------------------
# Template : OVIS Service Sync
# ----------------------------------------------------------------------------
SYNTAX_VERSION 4


SCHEDULE &quot;OVIS Service Sync&quot;
DESCRIPTION &quot;OVIS Service Model Synchronization&quot;
MINUTE &quot;0,5,10,15,20,25,30,35,40,45,50,55&quot;
SCHEDPROG &quot;/opt/OV/VPIS/bin/vpispull.sh n2app014.hphc.org&quot;
USER &quot;root&quot;
SEND_OUTPUT
SUCCESS
SET
SEVERITY Normal
MSGGRP &quot;OVIS&quot;
TEXT &quot;OVIS and OVO Sync completed successfully&quot;
FAILURE
SET

# ----------------------------------------------------------------------------
# Template : OpC User Log Maint
# ----------------------------------------------------------------------------
SYNTAX_VERSION 4


SCHEDULE &quot;OpC User Log Maint&quot;
DESCRIPTION &quot;Rolls IT/O User logs&quot;
MINUTE &quot;0&quot;
HOUR &quot;1,12&quot;
SCHEDPROG &quot;xeq logmaint.actions.ovopc&quot;
USER &quot;agent.ovopc,log&quot;
SEND_OUTPUT
SUCCESS
SET
SEVERITY Normal
MSGGRP &quot;OpC&quot;
TEXT &quot;IT/O User logs for MPE/iX have been rolled.&quot;
FAILURE
SET
SEVERITY Warning
TEXT &quot;Unable to roll IT/O User logs on MPE/iX&quot;

# ----------------------------------------------------------------------------
# Template : PingServer
# ----------------------------------------------------------------------------
SYNTAX_VERSION 4


SCHEDULE &quot;PingServer&quot;
DESCRIPTION &quot;Ping a server to see if it is up&quot;
MINUTE &quot;5&quot;
SCHEDPROG &quot;/opt/OV/scripts/PingSever N2DEV006&quot;
USER &quot;root&quot;
FAILURE
SET
SEVERITY Unknown
NODE IP 155.49.113.5 &quot;n2dev006.hphc.org&quot;



And here is the output I am getting:


Template------------------------------------------------------------------------------------------------------------------------------
Template# Template : PingServer

Template------------------------------------------------------------------------------------------------------------------------------
Template# ----------------------------------------------------------------------------

Template------------------------------------------------------------------------------------------------------------------------------
TemplateSYNTAX_VERSION 4

Template------------------------------------------------------------------------------------------------------------------------------
Template

Template------------------------------------------------------------------------------------------------------------------------------
Template

Template------------------------------------------------------------------------------------------------------------------------------
TemplateSCHEDULE &quot;PingServer&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template DESCRIPTION &quot;Ping a server to see if it is up&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template MINUTE &quot;5&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template SCHEDPROG &quot;/opt/OV/scripts/PingSever N2DEV006&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template USER &quot;root&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template FAILURE

Template------------------------------------------------------------------------------------------------------------------------------
Template SET

Template------------------------------------------------------------------------------------------------------------------------------
Template SEVERITY Unknown

Template------------------------------------------------------------------------------------------------------------------------------
Template NODE IP 155.49.113.5 &quot;n2dev006.hphc.org&quot;

Template------------------------------------------------------------------------------------------------------------------------------
Template




I want all the stuff between each 'Template&quot; in an array element.

Thanks, sorry for the long reply.
 
nfaber,

My mistake I put a backslash instead of a slash for the input record separator. It should be '$/'.
 
$/ = undef;

open (INFILE, &quot;< templrep.log&quot;);
$text = <INFILE>;
close INFILE;

$text =~ s/.+?(DESCRIPTION)//s;

@templates = split(/DESCRIPTION/, $text);

foreach (@templates) {
$counter++;
print &quot;\$templates[$counter] : $_&quot;;
}


Kind Regards
Duncan
 
That did it raider, and thanks as well Duncan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top