For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > complex parse question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author complex parse question
Nex_s

2005-07-26, 5:02 pm

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

alexandre.melard@gmail.com

2005-07-26, 5:02 pm

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

Nex_s

2005-07-26, 5:02 pm


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

alexandre.melard@gmail.com

2005-07-26, 5:02 pm

All right,

Give me 10mns I see if I get somewhere

Alexandre

alexandre.melard@gmail.com

2005-07-26, 5:02 pm

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

attn.steven.kuo@gmail.com

2005-07-26, 5:02 pm

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

John W. Krahn

2005-07-26, 5:02 pm

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
Anno Siegel

2005-07-27, 4:02 am

Nex_s <chrismm31@hotmail.com> 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.
Nex_s

2005-07-27, 9:03 am


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



alexandre.mel...@gmail.com 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


Nex_s

2005-07-27, 5:05 pm

Got it, thanks again to everyone, blood pressure returning to normal
:)

Nex_s wrote:[color=darkred]
> 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
>
>
>
> alexandre.mel...@gmail.com wrote:

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com