complex parse question

Discussion in 'Perl Misc' started by Nex_s, Jul 26, 2005.

  1. Nex_s

    Nex_s Guest

    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
    Nex_s, Jul 26, 2005
    #1
    1. Advertising

  2. Nex_s

    Guest

    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
    , Jul 26, 2005
    #2
    1. Advertising

  3. Nex_s

    Nex_s Guest

    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
    Nex_s, Jul 26, 2005
    #3
  4. Nex_s

    Guest

    All right,

    Give me 10mns I see if I get somewhere

    Alexandre
    , Jul 26, 2005
    #4
  5. Nex_s

    Guest

    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
    , Jul 26, 2005
    #5
  6. Nex_s

    Guest

    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
    , Jul 26, 2005
    #6
  7. 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
    John W. Krahn, Jul 26, 2005
    #7
  8. Nex_s

    Anno Siegel Guest

    Nex_s <> 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.
    Anno Siegel, Jul 27, 2005
    #8
  9. Nex_s

    Nex_s Guest

    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



    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, Jul 27, 2005
    #9
  10. Nex_s

    Nex_s Guest

    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
    >
    >
    >
    > 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, Jul 27, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    569
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    739
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    678
    Robert Mark Bram
    Feb 4, 2007
  4. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    755
  5. Alexander Kellett

    [BUG] parse error in complex heredocs

    Alexander Kellett, Feb 17, 2005, in forum: Ruby
    Replies:
    4
    Views:
    101
    Navindra Umanee
    Feb 17, 2005
Loading...

Share This Page