Reg:parsing a file

Discussion in 'Perl Misc' started by sivga, Sep 6, 2006.

  1. sivga

    sivga Guest

    hello experts,

    Iam new to perl .Can you pls suggest how to do this this is wat iam
    trying to do i have a text file with following lines


    exact 5
    seasad 3
    switch text 1.2
    new data 20
    cd data 10
    phase 30
    extra 40
    same 200
    different 500


    wat i need do is i neeed to read the file and if the file has (matches)



    switch text i need to write switch text to a new file say k2 and the
    next four lines new data ,cd data,extra and phase to k2 .other wise no
    need to write to the file k2. ,I cannot form the string say new data cd



    data and write to a file coz the numbers at the end will differ .How do



    i do it ?


    while (<AG>) {


    if ($_ =~ /exact +([0-9]+)/ ) {
    print k1 " $_";


    } elsif ( $_ =~ /switch text /) {
    print k2 "$_";




    } elsif ($_ =~ /same/) {
    print k1 "$_";
    }


    Thanks for the help
    sivga, Sep 6, 2006
    #1
    1. Advertising

  2. sivga

    Klaus Guest

    sivga wrote:
    > hello experts,
    >
    > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > trying to do i have a text file


    [snip]

    > wat i need do is i neeed to read the file


    [snip]

    > i need to write switch text to a new file say k2


    [snip]

    It is recommended that you begin your perl program with the following
    two lines:

    use strict;
    use warnings;

    Secondly, you need to open all your input-file and output-files.
    Have a look at "perldoc perlopen" on how to open files.

    Personally, I would say:

    open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";

    > while (<AG>) {

    while (<$AG>) {

    > print k1 " $_";

    print {$k1} ' ', $_;

    > print k2 "$_";

    print {$k2} $_;

    > print k1 "$_";

    print {$k1} $_;
    Klaus, Sep 6, 2006
    #2
    1. Advertising

  3. sivga

    sivga Guest

    Klaus wrote:
    > sivga wrote:
    > > hello experts,
    > >
    > > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > > trying to do i have a text file

    >
    > [snip]
    >
    > > wat i need do is i neeed to read the file

    >
    > [snip]
    >
    > > i need to write switch text to a new file say k2

    >
    > [snip]
    >
    > It is recommended that you begin your perl program with the following
    > two lines:
    >
    > use strict;
    > use warnings;
    >
    > Secondly, you need to open all your input-file and output-files.
    > Have a look at "perldoc perlopen" on how to open files.
    >
    > Personally, I would say:
    >
    > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    > open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
    >
    > > while (<AG>) {

    > while (<$AG>) {
    >
    > > print k1 " $_";

    > print {$k1} ' ', $_;
    >
    > > print k2 "$_";

    > print {$k2} $_;
    >
    > > print k1 "$_";

    > print {$k1} $_;


    klaus i have already done all those ..i have pasted jus the snippet of
    the code . The problem is there a way to specify like if the condition
    matches then get next four lines .how do i specify this

    thanks for the help
    sivga, Sep 6, 2006
    #3
  4. sivga

    sivga Guest

    sivga wrote:
    > Klaus wrote:
    > > sivga wrote:
    > > > hello experts,
    > > >
    > > > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > > > trying to do i have a text file

    > >
    > > [snip]
    > >
    > > > wat i need do is i neeed to read the file

    > >
    > > [snip]
    > >
    > > > i need to write switch text to a new file say k2

    > >
    > > [snip]
    > >
    > > It is recommended that you begin your perl program with the following
    > > two lines:
    > >
    > > use strict;
    > > use warnings;
    > >
    > > Secondly, you need to open all your input-file and output-files.
    > > Have a look at "perldoc perlopen" on how to open files.
    > >
    > > Personally, I would say:
    > >
    > > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > > open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    > > open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
    > >
    > > > while (<AG>) {

    > > while (<$AG>) {
    > >
    > > > print k1 " $_";

    > > print {$k1} ' ', $_;
    > >
    > > > print k2 "$_";

    > > print {$k2} $_;
    > >
    > > > print k1 "$_";

    > > print {$k1} $_;

    >
    > klaus i have already done all those ..i have pasted jus the snippet of
    > the code . The problem is there a way to specify like if the condition
    > matches then get next four lines .how do i specify this
    >
    > thanks for the help


    wat i need do is if th eline matches then i need to get the next four
    lines an place it in a array ..how do i do it in perl ?
    sivga, Sep 6, 2006
    #4
  5. sivga

    Klaus Guest

    sivga wrote:
    > sivga wrote:
    > > Klaus wrote:
    > > > sivga wrote:
    > > > > hello experts,
    > > > >
    > > > > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > > > > trying to do i have a text file
    > > >
    > > > [snip]
    > > >
    > > > > wat i need do is i neeed to read the file
    > > >
    > > > [snip]
    > > >
    > > > > i need to write switch text to a new file say k2
    > > >
    > > > [snip]
    > > >
    > > > It is recommended that you begin your perl program with the following
    > > > two lines:
    > > >
    > > > use strict;
    > > > use warnings;
    > > >
    > > > Secondly, you need to open all your input-file and output-files.
    > > > Have a look at "perldoc perlopen" on how to open files.
    > > >
    > > > Personally, I would say:
    > > >
    > > > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > > > open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    > > > open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
    > > >
    > > > > while (<AG>) {
    > > > while (<$AG>) {
    > > >
    > > > > print k1 " $_";
    > > > print {$k1} ' ', $_;
    > > >
    > > > > print k2 "$_";
    > > > print {$k2} $_;
    > > >
    > > > > print k1 "$_";
    > > > print {$k1} $_;

    > >
    > > klaus i have already done all those ..i have pasted jus the snippet of
    > > the code .


    then please post some code that you have already done, not something
    else which is different to what you have already done.

    > wat i need do is if th eline matches then i need to get the next four
    > lines an place it in a array ..how do i do it in perl ?


    I will give an example to match the string "abc" (but be aware that you
    will get "Use of uninitialized value" warnings if, after a successful
    match /abc/, there are less than 4 lines in the file left):

    use strict;
    use warnings;

    open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";

    my @array;
    while (<$AG>) { chomp;
    if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
    }
    }

    { local $, = "\n"; print @array, '' }
    Klaus, Sep 6, 2006
    #5
  6. sivga

    sivga Guest

    Klaus wrote:
    > sivga wrote:
    > > sivga wrote:
    > > > Klaus wrote:
    > > > > sivga wrote:
    > > > > > hello experts,
    > > > > >
    > > > > > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > > > > > trying to do i have a text file
    > > > >
    > > > > [snip]
    > > > >
    > > > > > wat i need do is i neeed to read the file
    > > > >
    > > > > [snip]
    > > > >
    > > > > > i need to write switch text to a new file say k2
    > > > >
    > > > > [snip]
    > > > >
    > > > > It is recommended that you begin your perl program with the following
    > > > > two lines:
    > > > >
    > > > > use strict;
    > > > > use warnings;
    > > > >
    > > > > Secondly, you need to open all your input-file and output-files.
    > > > > Have a look at "perldoc perlopen" on how to open files.
    > > > >
    > > > > Personally, I would say:
    > > > >
    > > > > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > > > > open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    > > > > open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
    > > > >
    > > > > > while (<AG>) {
    > > > > while (<$AG>) {
    > > > >
    > > > > > print k1 " $_";
    > > > > print {$k1} ' ', $_;
    > > > >
    > > > > > print k2 "$_";
    > > > > print {$k2} $_;
    > > > >
    > > > > > print k1 "$_";
    > > > > print {$k1} $_;
    > > >
    > > > klaus i have already done all those ..i have pasted jus the snippet of
    > > > the code .

    >
    > then please post some code that you have already done, not something
    > else which is different to what you have already done.
    >
    > > wat i need do is if th eline matches then i need to get the next four
    > > lines an place it in a array ..how do i do it in perl ?

    >
    > I will give an example to match the string "abc" (but be aware that you
    > will get "Use of uninitialized value" warnings if, after a successful
    > match /abc/, there are less than 4 lines in the file left):
    >
    > use strict;
    > use warnings;
    >
    > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    >
    > my @array;
    > while (<$AG>) { chomp;
    > if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
    > }
    > }
    >
    > { local $, = "\n"; print @array, '' }



    thanks klaus for the help actually i was trying different ways to
    handle this .thanks for the help
    sivga, Sep 6, 2006
    #6
  7. sivga

    sivga Guest

    sivga wrote:
    > Klaus wrote:
    > > sivga wrote:
    > > > sivga wrote:
    > > > > Klaus wrote:
    > > > > > sivga wrote:
    > > > > > > hello experts,
    > > > > > >
    > > > > > > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > > > > > > trying to do i have a text file
    > > > > >
    > > > > > [snip]
    > > > > >
    > > > > > > wat i need do is i neeed to read the file
    > > > > >
    > > > > > [snip]
    > > > > >
    > > > > > > i need to write switch text to a new file say k2
    > > > > >
    > > > > > [snip]
    > > > > >
    > > > > > It is recommended that you begin your perl program with the following
    > > > > > two lines:
    > > > > >
    > > > > > use strict;
    > > > > > use warnings;
    > > > > >
    > > > > > Secondly, you need to open all your input-file and output-files.
    > > > > > Have a look at "perldoc perlopen" on how to open files.
    > > > > >
    > > > > > Personally, I would say:
    > > > > >
    > > > > > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > > > > > open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
    > > > > > open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
    > > > > >
    > > > > > > while (<AG>) {
    > > > > > while (<$AG>) {
    > > > > >
    > > > > > > print k1 " $_";
    > > > > > print {$k1} ' ', $_;
    > > > > >
    > > > > > > print k2 "$_";
    > > > > > print {$k2} $_;
    > > > > >
    > > > > > > print k1 "$_";
    > > > > > print {$k1} $_;
    > > > >
    > > > > klaus i have already done all those ..i have pasted jus the snippet of
    > > > > the code .

    > >
    > > then please post some code that you have already done, not something
    > > else which is different to what you have already done.
    > >
    > > > wat i need do is if th eline matches then i need to get the next four
    > > > lines an place it in a array ..how do i do it in perl ?

    > >
    > > I will give an example to match the string "abc" (but be aware that you
    > > will get "Use of uninitialized value" warnings if, after a successful
    > > match /abc/, there are less than 4 lines in the file left):
    > >
    > > use strict;
    > > use warnings;
    > >
    > > open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
    > >
    > > my @array;
    > > while (<$AG>) { chomp;
    > > if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
    > > }
    > > }
    > >
    > > { local $, = "\n"; print @array, '' }

    >
    >
    > thanks klaus for the help actually i was trying different ways to
    > handle this .thanks for the help



    klaus thanks for the help but can help in undersatnding how does the
    for loop work here when u said for (1..4) Can you pls help here
    ..thanks
    sivga, Sep 6, 2006
    #7
  8. sivga <> wrote:
    > hello experts,
    >
    > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > trying to do i have a text file with following lines
    >
    >
    > exact 5
    > seasad 3
    > switch text 1.2
    > new data 20
    > cd data 10
    > phase 30
    > extra 40
    > same 200
    > different 500
    >
    >
    > wat i need do is i neeed to read the file and if the file has (matches)
    >
    >
    >
    > switch text i need to write switch text to a new file say k2 and the
    > next four lines new data ,cd data,extra and phase to k2 .other wise no
    > need to write to the file k2. ,I cannot form the string say new data cd
    >
    >
    >
    > data and write to a file coz the numbers at the end will differ .How do
    >
    >
    >
    > i do it ?
    >
    >
    > while (<AG>) {
    >
    >
    > if ($_ =~ /exact +([0-9]+)/ ) {
    > print k1 " $_";
    >
    >
    > } elsif ( $_ =~ /switch text /) {
    > print k2 "$_";



    print k2 scalar(<AG>); # print the next 4 lines
    print k2 scalar(<AG>);
    print k2 scalar(<AG>);
    print k2 scalar(<AG>);

    >
    >
    >
    > } elsif ($_ =~ /same/) {
    > print k1 "$_";
    > }



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 6, 2006
    #8
  9. sivga <> wrote:


    [ Please trim quoted text that is not needed to establish the
    context for your comments.
    ]


    > how does the
    > for loop work here when u said for (1..4)



    See the "Foreach Loops" section in:

    perldoc perlsyn


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 6, 2006
    #9
  10. sivga

    Mumia W. Guest

    On 09/06/2006 11:42 AM, sivga wrote:
    > hello experts,
    >
    > Iam new to perl .Can you pls suggest how to do this this is wat iam
    > trying to do i have a text file with following lines
    >
    >
    > exact 5
    > seasad 3
    > switch text 1.2
    > new data 20
    > cd data 10
    > phase 30
    > extra 40
    > same 200
    > different 500
    >
    >
    > wat i need do is i neeed to read the file and if the file has (matches)
    >
    >
    >
    > switch text i need to write switch text to a new file say k2 and the
    > next four lines new data ,cd data,extra and phase to k2 .other wise no
    > need to write to the file k2. ,I cannot form the string say new data cd
    >
    >
    >
    > data and write to a file coz the numbers at the end will differ .How do
    >
    >
    >
    > i do it ?
    >
    >
    > while (<AG>) {
    >
    >
    > if ($_ =~ /exact +([0-9]+)/ ) {
    > print k1 " $_";
    >
    >
    > } elsif ( $_ =~ /switch text /) {
    > print k2 "$_";
    >
    >
    >
    >
    > } elsif ($_ =~ /same/) {
    > print k1 "$_";
    > }
    >
    >
    > Thanks for the help
    >


    It was incredibly difficult to understand you, but I think I
    know what you're getting at (thanks to the code you posted).

    It is probably going to be easier for you to use Tie::File to
    make the file you are reading look like an array. Then reading
    the next four lines should be easy:

    The following is NOT an example of such because I decided to
    put the data directly in my program:

    use strict;
    use warnings;
    my (@AR, @K1, @K2);
    @AR = q{
    exact 5
    seasad 3
    switch text 1.2
    new data 20
    cd data 10
    phase 30
    extra 40
    same 200
    different 500
    } =~ m/^ *(\w.*)$/mg;

    local $\ = "\n";
    for my $nx (0..$#AR) {
    $_ = $AR[$nx];
    if (/switch text/) {
    push @K2, @AR[$nx..$nx+4];
    } elsif (/exact/) {
    push @K1, $_;
    } elsif (/same/) {
    push @K1, $_;
    }
    }

    print join "\n", @K1;
    print '----------------------------';
    print join "\n", @K2;

    __END__

    Read 'perldoc perltie' and 'perldoc Tie::File' to see how to
    use tied file arrays.
    Mumia W., Sep 7, 2006
    #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. ashutosh
    Replies:
    1
    Views:
    1,613
    Wendy S
    Apr 22, 2005
  2. praba kar

    reg php.ini equivalent file in python

    praba kar, Jun 7, 2005, in forum: Python
    Replies:
    2
    Views:
    617
    Magnus Lycka
    Jun 8, 2005
  3. sivga

    Reg parsing Design

    sivga, Nov 9, 2006, in forum: Java
    Replies:
    5
    Views:
    293
    Mark Jeffcoat
    Nov 9, 2006
  4. kumsa

    Reg Xml Parsing

    kumsa, Jan 9, 2007, in forum: Java
    Replies:
    2
    Views:
    580
    Daniel Pitts
    Jan 14, 2007
  5. vikkous

    Lalr(n) parsing with reg

    vikkous, Apr 25, 2005, in forum: Ruby
    Replies:
    1
    Views:
    147
    Florian G. Pflug
    Apr 26, 2005
Loading...

Share This Page