FAQ
Hi,

I am trying to process a code for some processing.
The code looks like

keywordx ...

keywordy identifier_a
some text
endkeywordy

keywordz identifier_a
some text
endkeywordz

endkeywordx

From this, I would like to extract text starting from keywordy and endkeywordy.
Is using text balanced module the right way to go?
An example with or without text-balanced module would be really helpful ...

Regards

Search Discussions

  • Rob Dixon at Oct 10, 2008 at 4:19 pm

    Sharan Basappa wrote:

    I am trying to process a code for some processing.
    The code looks like

    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
    From this, I would like to extract text starting from keywordy and endkeywordy.
    Is using text balanced module the right way to go?
    An example with or without text-balanced module would be really helpful ...
    If your problem really is that simple, and you don't expect 'keywordy' blocks to
    be nested inside other 'keywordy' blocks, then the program below will do what
    you need.

    It may help if you showed us some actual data so that we could get a better
    insight into how it behaves.

    HTH,

    Rob


    use strict;
    use warnings;

    while (<DATA>) {
    print if /\bkeywordy\b/ .. /\bendkeywordy\b/;
    }

    __DATA__
    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
  • Sharan Basappa at Oct 14, 2008 at 3:08 pm

    On Fri, Oct 10, 2008 at 9:49 PM, Rob Dixon wrote:
    Sharan Basappa wrote:
    I am trying to process a code for some processing.
    The code looks like

    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
    From this, I would like to extract text starting from keywordy and endkeywordy.
    Is using text balanced module the right way to go?
    An example with or without text-balanced module would be really helpful ...
    If your problem really is that simple, and you don't expect 'keywordy' blocks to
    be nested inside other 'keywordy' blocks, then the program below will do what
    you need.

    It may help if you showed us some actual data so that we could get a better
    insight into how it behaves.

    HTH,

    Rob


    use strict;
    use warnings;

    while (<DATA>) {
    print if /\bkeywordy\b/ .. /\bendkeywordy\b/;
    }

    __DATA__
    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
    Thanks, Rob. I will give a try with this code. But given that there
    are new lines between keyword and endkeyword,
    do you think it will work.

    Regards
  • Rob Dixon at Oct 14, 2008 at 3:21 pm

    Sharan Basappa wrote:
    On Fri, Oct 10, 2008 at 9:49 PM, Rob Dixon wrote:

    If your problem really is that simple, and you don't expect 'keywordy' blocks to
    be nested inside other 'keywordy' blocks, then the program below will do what
    you need.

    It may help if you showed us some actual data so that we could get a better
    insight into how it behaves.


    use strict;
    use warnings;

    while (<DATA>) {
    print if /\bkeywordy\b/ .. /\bendkeywordy\b/;
    }

    __DATA__
    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
    Thanks, Rob. I will give a try with this code. But given that there
    are new lines between keyword and endkeyword,
    do you think it will work.
    I am certain that it will work if the data is similar to what you have
    described. I hope it is obvious that my program reads the input file line by
    line, and you would need to write

    open my $fh, '<', 'filename' or die $!;

    while (<$fh>) {
    :
    }

    instead of using the DATA file handle as I did in my example.

    Rob
  • Sharan Basappa at Oct 14, 2008 at 3:39 pm

    On Tue, Oct 14, 2008 at 8:50 PM, Rob Dixon wrote:
    Sharan Basappa wrote:
    On Fri, Oct 10, 2008 at 9:49 PM, Rob Dixon wrote:

    If your problem really is that simple, and you don't expect 'keywordy' blocks to
    be nested inside other 'keywordy' blocks, then the program below will do what
    you need.

    It may help if you showed us some actual data so that we could get a better
    insight into how it behaves.


    use strict;
    use warnings;

    while (<DATA>) {
    print if /\bkeywordy\b/ .. /\bendkeywordy\b/;
    }

    __DATA__
    keywordx ...

    keywordy identifier_a
    some text
    endkeywordy

    keywordz identifier_a
    some text
    endkeywordz

    endkeywordx
    Thanks, Rob. I will give a try with this code. But given that there
    are new lines between keyword and endkeyword,
    do you think it will work.
    I am certain that it will work if the data is similar to what you have
    described. I hope it is obvious that my program reads the input file line by
    line, and you would need to write

    open my $fh, '<', 'filename' or die $!;

    while (<$fh>) {
    :
    }

    instead of using the DATA file handle as I did in my example.

    Rob
    Thanks, Rob. I am aware that a file descriptor needs to be opened etc.
    BTW, do you think text-balanced can help in situations that are slightly
    more complex than I described above. For example, if I have data like:

    keywordx id
    some text
    some text
    {
    more text
    more text
    }

    I would like to extract starting from keywordx till closing braces.
    Add to this, there will be multiple instances of such code pieces in the
    input I am trying to process.

    I looked at text-balanced but could not find anything that would help in doing
    the above task.

    Thanks once again ...
  • Rob Dixon at Oct 14, 2008 at 5:51 pm

    Sharan Basappa wrote:

    I am aware that a file descriptor needs to be opened etc. BTW, do you think
    text-balanced can help in situations that are slightly more complex than I
    described above. For example, if I have data like:

    keywordx id
    some text
    some text
    {
    more text
    more text
    }

    I would like to extract starting from keywordx till closing braces.
    Add to this, there will be multiple instances of such code pieces in the
    input I am trying to process.

    I looked at text-balanced but could not find anything that would help in doing
    the above task.
    No, I agree. It looks like the module could do what you want, but I doubt if it
    would be any better for your purpose than native Perl code and specifically the
    range operator.

    It is very hard to advise you properly without seeing the real data that you
    need to process. It looks like it may be C code, but I hope you can show a
    proper example before we help you any further.

    Rob

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedOct 10, '08 at 3:56p
activeOct 14, '08 at 5:51p
posts6
users2
websiteperl.org

2 users in discussion

Sharan Basappa: 3 posts Rob Dixon: 3 posts

People

Translate

site design / logo © 2021 Grokbase