Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > complex parse question

Reply
Thread Tools

complex parse question

 
 
Nex_s
Guest
Posts: n/a
 
      07-26-2005
Hi All,

Perhaps this is easier than I think but I need to parse a text file
and save it back to a text file. Description below:

I need the top portion or file header up to the word "ANNOVFL"
For each of the follow records I need to look for anything with a title
that starts with "NSP" and capture it's relevent data. For example I'd
need each occurance of the following data, 4 lines in total per
occurance :

38 NSPOWERANN1
30
0 0




Any help would be greatly appreciated.
Chris

#----------------- DATA --------------------------

BISH OMPR225 JUL20 13:25:28 0001 INFO OM REPORT
CLASS: NSPI
START:2005/07/20 13:20:00 WED; STOP: 2005/07/20 13:25:00 WED;
SLOWSAMPLES: 3 ; FASTSAMPLES: 30 ;

TRK
KEY (COMMON_LANGUAGE_NAME)
INFO (OM2TRKINFO)
NATTMPT NOVFLATB CONNECT

724 EMERAPRI2W
2W 94 94
23 0 23

------------------------------------------------------------------
6401 76 6302

ANN
KEY (COMMON_LANGUAGE_NAME)
INFO (ANN_OMINFO)
ANNATT ANNOVFL

37 NSCCADMQTA
100
0 0

38 NSPOWERANN1
30
0 0

39 DSCWIDRMDR
30
0 0

40 UCDQTA
200
19 0

41 UCDNSA
15
1 0

268 NSPCALANN23
255
0 0

269 NSPCHEANN24
255
0 0

270 NSPLIVANN25
255
0 0

271 NSPKINANN26
255
0 0

 
Reply With Quote
 
 
 
 
alexandre.melard@gmail.com
Guest
Posts: n/a
 
      07-26-2005
Hi Chris,

Could you be a little more specific?
What do you want to do with the :

271 NSPKINANN26
255
0 0

bits?

Do you want to store them in memory, in files, in one file? I do not
see your problem.

Alexandre Melard

 
Reply With Quote
 
 
 
 
Nex_s
Guest
Posts: n/a
 
      07-26-2005

Hi Alexandre,

Sorry about that my apologies. The data is in a text file called
"ivr_dms.txt" . I want to open the file, parse it accordingly, and
save it back out to the same file name. The finished product should
look like the following (note in the bottom portion of the file after
the word "ANNOVFL" only those entries that start with NSP are
included).

Thank you,
Chris





BISH OMPR225 JUL20 13:25:28 0001 INFO OM REPORT
CLASS: NSPI
START:2005/07/20 13:20:00 WED; STOP: 2005/07/20 13:25:00 WED;
SLOWSAMPLES: 3 ; FASTSAMPLES: 30 ;

TRK
KEY (COMMON_LANGUAGE_NAME)
INFO (OM2TRKINFO)
NATTMPT NOVFLATB CONNECT

724 EMERAPRI2W
2W 94 94
23 0 23

------------------------------------------------------------------
6401 76 6302

ANN
KEY (COMMON_LANGUAGE_NAME)
INFO (ANN_OMINFO)
ANNATT ANNOVFL

38 NSPOWERANN1
30
0 0

268 NSPCALANN23
255
0 0

269 NSPCHEANN24
255
0 0

270 NSPLIVANN25
255
0 0

271 NSPKINANN26
255
0 0

 
Reply With Quote
 
alexandre.melard@gmail.com
Guest
Posts: n/a
 
      07-26-2005
All right,

Give me 10mns I see if I get somewhere

Alexandre

 
Reply With Quote
 
alexandre.melard@gmail.com
Guest
Posts: n/a
 
      07-26-2005
Try this out:

#!/usr/bin/perl -w

use strict;

my $ok = 1;
my $count = 0;
open OUT, ">save.txt";
open FILE, "data.txt";
while (<FILE>) {
if(/ANNOVFL/) { $ok = 0;}
if($ok) {
print OUT;
}else {
if(/^\s+\d+\s+NSP/) {
$count = 3;
}
if ($count) {
print OUT;
$count--;
}
}
}
close FILE;
close OUT;

Tell me if that is what you're looking for...

Alexandre

 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      07-26-2005
Nex_s wrote:
> Hi Alexandre,
>
> Sorry about that my apologies. The data is in a text file called
> "ivr_dms.txt" . I want to open the file, parse it accordingly, and
> save it back out to the same file name. The finished product should
> look like the following (note in the bottom portion of the file after
> the word "ANNOVFL" only those entries that start with NSP are
> included).
>
> Thank you,
> Chris




You can use regular expressions and the
range operator to print selective
portions of the file.

#!/usr/bin/perl
use strict;
use warnings;

{
local $^I = 1;
local *_;
while (<>)
{
if ($. == 1 ... /ANNOVFL/)
{
print;
next;
}
print if (/\d NSP/ ... /^\s*$/);
}
}


--
Hope this helps,
Steven

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-26-2005
Nex_s wrote:
>
> Sorry about that my apologies. The data is in a text file called
> "ivr_dms.txt" . I want to open the file, parse it accordingly, and
> save it back out to the same file name. The finished product should
> look like the following (note in the bottom portion of the file after
> the word "ANNOVFL" only those entries that start with NSP are
> included).
>
>
> BISH OMPR225 JUL20 13:25:28 0001 INFO OM REPORT
> CLASS: NSPI
> START:2005/07/20 13:20:00 WED; STOP: 2005/07/20 13:25:00 WED;
> SLOWSAMPLES: 3 ; FASTSAMPLES: 30 ;
>
> TRK
> KEY (COMMON_LANGUAGE_NAME)
> INFO (OM2TRKINFO)
> NATTMPT NOVFLATB CONNECT
>
> 724 EMERAPRI2W
> 2W 94 94
> 23 0 23
>
> ------------------------------------------------------------------
> 6401 76 6302
>
> ANN
> KEY (COMMON_LANGUAGE_NAME)
> INFO (ANN_OMINFO)
> ANNATT ANNOVFL
>
> 38 NSPOWERANN1
> 30
> 0 0
>
> 268 NSPCALANN23
> 255
> 0 0
>
> 269 NSPCHEANN24
> 255
> 0 0
>
> 270 NSPLIVANN25
> 255
> 0 0
>
> 271 NSPKINANN26
> 255
> 0 0
>



perl -i.bak -ne'print 1../ANNOVFL/?$_:/^\s+\d+\s+NSP/?$_:""' ivr_dms.txt


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-27-2005
Nex_s <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Hi Alexandre,
>
> Sorry about that my apologies. The data is in a text file called
> "ivr_dms.txt" . I want to open the file, parse it accordingly, and
> save it back out to the same file name. The finished product should
> look like the following (note in the bottom portion of the file after
> the word "ANNOVFL" only those entries that start with NSP are
> included).


It looks like you could use paragraph mode for these data. See
$/ in perlvar for more on paragraph mode.

Assuming a __DATA__ section with the content of your original file:

$/ = ''; # set paragraph mode

# extract header
while ( <DATA> ) {
print;
last if /ANNOVFL/;
}

# extract wanted data
/^\s*\d+\s+NSP/ and print while <DATA>;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Nex_s
Guest
Posts: n/a
 
      07-27-2005

Thank you Alexandre and to all who replied it's working great. The
only other thing is I need the word "ANNOVFL" and an extra line
included. I can probably figure that out but if someone knows, that'd
be great.

Chris



(E-Mail Removed) wrote:
> Try this out:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $ok = 1;
> my $count = 0;
> open OUT, ">save.txt";
> open FILE, "data.txt";
> while (<FILE>) {
> if(/ANNOVFL/) { $ok = 0;}
> if($ok) {
> print OUT;
> }else {
> if(/^\s+\d+\s+NSP/) {
> $count = 3;
> }
> if ($count) {
> print OUT;
> $count--;
> }
> }
> }
> close FILE;
> close OUT;
>
> Tell me if that is what you're looking for...
>
> Alexandre


 
Reply With Quote
 
Nex_s
Guest
Posts: n/a
 
      07-27-2005
Got it, thanks again to everyone, blood pressure returning to normal


Nex_s wrote:
> Thank you Alexandre and to all who replied it's working great. The
> only other thing is I need the word "ANNOVFL" and an extra line
> included. I can probably figure that out but if someone knows, that'd
> be great.
>
> Chris
>
>
>
> (E-Mail Removed) wrote:
> > Try this out:
> >
> > #!/usr/bin/perl -w
> >
> > use strict;
> >
> > my $ok = 1;
> > my $count = 0;
> > open OUT, ">save.txt";
> > open FILE, "data.txt";
> > while (<FILE>) {
> > if(/ANNOVFL/) { $ok = 0;}
> > if($ok) {
> > print OUT;
> > }else {
> > if(/^\s+\d+\s+NSP/) {
> > $count = 3;
> > }
> > if ($count) {
> > print OUT;
> > $count--;
> > }
> > }
> > }
> > close FILE;
> > close OUT;
> >
> > Tell me if that is what you're looking for...
> >
> > Alexandre


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
[BUG] parse error in complex heredocs Alexander Kellett Ruby 4 02-17-2005 06:22 PM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments