complex parse question

N

Nex_s

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
 
A

alexandre.melard

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
 
N

Nex_s

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
 
A

alexandre.melard

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
 
A

attn.steven.kuo

Nex_s said:
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*$/);
}
}
 
J

John W. Krahn

Nex_s said:
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
 
A

Anno Siegel

Nex_s said:
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
 
N

Nex_s

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top