printing two consecutive lines

Discussion in 'Perl Misc' started by icebrakers2008@gmail.com, Aug 21, 2008.

  1. Guest

    Hi,

    I am new to perl, I have a sample file like this
    ***********************************************************************************
    Path 11: VIOLATED Hold Check with Pin core1_2/tx_lr/ifft128/
    u7_mem_fft_0/u5_
    data_ram_5/\mem_reg[8][13] /CP
    Endpoint: core1_2/tx_lr/ifft128/u7_mem_fft_0/u5_data_ram_5/\mem_r
    eg[8][13] /D (v) checked with leading edge of 'x_clk'
    Beginpoint: core1_2/tx_lr/ifft128/u4_fft_core_top_0/
    u0_fft_mux_0/\dout_5_
    reg[13] /Q (v) triggered by leading edge of 'x_clk'
    Path Groups: {reg2reg}
    Other End Arrival Time 0.805
    + Hold -0.017
    + Phase Shift 0.000
    - CPPR Adjustment 0.055
    + Uncertainty 0.100
    = Required Time 0.834
    Arrival Time 0.795
    Slack Time -0.039
    Clock Rise Edge 0.000
    = Beginpoint Arrival Time 0.000
    Timing Path:
    ****************************************************************************************

    I need to print whole line which starts with 'Endpoint' - But this
    line is spread in two lines as shown above, I want to print this in
    the same line. The whole file contains thousands of similar lines. I
    could able to print the line by searching "Endpoint" - but it only
    gives half the line and other half is in next line, which I am not
    able to print.

    Here is what I have:

    #!/usr/bin/perl
    use warnings;
    use strict;
    my $fh;
    open($fh, "<test2.rpt") - # test2.rpt is the file containing lines I
    mentioned above.
    or die "Can't open: $!";
    while (my $line = <$fh>) # was $line
    {
    if ($line=~m/Endpoint:/) {
    print "\n";
    #print "$1";
    print "$line";
    }

    }

    Appreciate your help.

    - SR
    , Aug 21, 2008
    #1
    1. Advertising

  2. writes:

    > I am new to perl


    Welcome aboard.

    > , I have a sample file like this
    > ***********************************************************************************
    > Path 11: VIOLATED Hold Check with Pin core1_2/tx_lr/ifft128/
    > u7_mem_fft_0/u5_
    > data_ram_5/\mem_reg[8][13] /CP
    > Endpoint: core1_2/tx_lr/ifft128/u7_mem_fft_0/u5_data_ram_5/\mem_r
    > eg[8][13] /D (v) checked with leading edge of 'x_clk'
    > Beginpoint: core1_2/tx_lr/ifft128/u4_fft_core_top_0/
    > u0_fft_mux_0/\dout_5_
    > reg[13] /Q (v) triggered by leading edge of 'x_clk'
    > Path Groups: {reg2reg}
    > Other End Arrival Time 0.805
    > + Hold -0.017
    > + Phase Shift 0.000
    > - CPPR Adjustment 0.055
    > + Uncertainty 0.100
    > = Required Time 0.834
    > Arrival Time 0.795
    > Slack Time -0.039
    > Clock Rise Edge 0.000
    > = Beginpoint Arrival Time 0.000
    > Timing Path:
    > ****************************************************************************************
    >
    > I need to print whole line which starts with 'Endpoint' - But this
    > line is spread in two lines as shown above, I want to print this in
    > the same line. The whole file contains thousands of similar lines. I
    > could able to print the line by searching "Endpoint" - but it only
    > gives half the line and other half is in next line, which I am not
    > able to print.
    >
    > Here is what I have:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;


    Warnings and strictures: Excellent!

    > my $fh;
    > open($fh, "<test2.rpt") - # test2.rpt is the file containing lines I
    > mentioned above.
    > or die "Can't open: $!";


    Not just checking the open, but also printing $! to describe the
    error. Also excellent!

    > while (my $line = <$fh>) # was $line
    > {
    > if ($line=~m/Endpoint:/) {
    > print "\n";
    > #print "$1";
    > print "$line";


    That should be:
    print $line;

    But don't worry - always quoting is a common mistake. The reasons why
    it's generally a bad idea are in the FAQ:

    perldoc -q quoting

    > }
    >
    > }


    Is the "Endpoint" *always* two lines - never more nor less? If so, you
    could simply read the second line separately:

    if ($line =~ /Endpoint:/) {
    my $line2 = <$fh>;
    print $line, $line2;
    }

    sherm--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherm Pendley, Aug 21, 2008
    #2
    1. Advertising

  3. wrote in
    news::

    > I am new to perl, I have a sample file like this


    Sample input snipped here for brevity ... Included with solution below.

    > Here is what I have:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > my $fh;
    > open($fh, "<test2.rpt") - # test2.rpt is the file containing lines I
    > mentioned above.
    > or die "Can't open: $!";


    Thanks for using warnings and strict as well as checking the return
    value of open.

    You could write the open call more succinctly as:

    my $filename = 'test2.rpt';
    open my $fh, '<', $filename
    or die "Cannot open '$filename': $!";

    > while (my $line = <$fh>) # was $line
    > {
    > if ($line=~m/Endpoint:/) {
    > print "\n";
    > #print "$1";
    > print "$line";
    > }
    > }


    #!/usr/bin/perl

    use strict;
    use warnings;

    while ( my $line = <DATA> ) {
    next unless $line =~ /^Endpoint:/;
    print $line;
    while ( my $next = <DATA> ) {
    last if $next =~ /^Beginpoint:/;
    print $next;
    }
    }

    __DATA__
    ************************************************************************
    ***********
    Path 11: VIOLATED Hold Check with Pin core1_2/tx_lr/ifft128/
    u7_mem_fft_0/u5_
    data_ram_5/\mem_reg[8][13] /CP
    Endpoint: core1_2/tx_lr/ifft128/u7_mem_fft_0/u5_data_ram_5/\mem_r
    eg[8][13] /D (v) checked with leading edge of 'x_clk'
    Beginpoint: core1_2/tx_lr/ifft128/u4_fft_core_top_0/
    u0_fft_mux_0/\dout_5_
    reg[13] /Q (v) triggered by leading edge of 'x_clk'
    Path Groups: {reg2reg}
    Other End Arrival Time 0.805
    + Hold -0.017
    + Phase Shift 0.000
    - CPPR Adjustment 0.055
    + Uncertainty 0.100
    = Required Time 0.834
    Arrival Time 0.795
    Slack Time -0.039
    Clock Rise Edge 0.000
    = Beginpoint Arrival Time 0.000
    Timing Path:
    ************************************************************************
    ****************

    __END__

    C:\Temp> t
    Endpoint: core1_2/tx_lr/ifft128/u7_mem_fft_0/u5_data_ram_5/\mem_r
    eg[8][13] /D (v) checked with leading edge of 'x_clk'

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Aug 21, 2008
    #3
  4. cartercc Guest

    On Aug 20, 8:08 pm, wrote:
    > I need to print whole line which starts with 'Endpoint'  - But this
    > line is spread in two lines as shown above, I want to print this in
    > the same line. The whole file contains thousands of similar lines.  I
    > could able to print the line by searching "Endpoint" - but it only
    > gives half the line and other half is in next line, which I am not
    > able to print.


    Set a flag that by default is false, turns to true when it hits
    'Endpoint:' and turns to false when it hits the next 'Beginpoint:' and
    print if $flag

    #!/usr/bin/perl
    use strict;
    my $flag = 0;

    open INFILE, "<test.txt" or die "Can't open: $!";
    while (<INFILE>)
    {
    if ($_ =~ /^Endpoint:/ and $flag == 0) {$flag = 1; }
    elsif ($_ =~ /Beginpoint:/ and $flag == 1) {$flag = 0; }
    print if $flag;
    }
    close INFILE;
    exit;
    cartercc, Aug 21, 2008
    #4
  5. cartercc <> writes:

    > Set a flag that by default is false, turns to true when it hits
    > 'Endpoint:' and turns to false when it hits the next 'Beginpoint:' and
    > print if $flag
    >
    > #!/usr/bin/perl
    > use strict;
    > my $flag = 0;
    >
    > open INFILE, "<test.txt" or die "Can't open: $!";
    > while (<INFILE>)
    > {
    > if ($_ =~ /^Endpoint:/ and $flag == 0) {$flag = 1; }
    > elsif ($_ =~ /Beginpoint:/ and $flag == 1) {$flag = 0; }
    > print if $flag;
    > }


    Why reinvent the wheel? Perl already has a built-in flip-flop operator
    that does exactly what you describe:

    while (<INFILE>) {
    print if ( /^Endpoint:/ .. /^Beginpoint:/ );
    }

    Details can be found in "perldoc perlop", under "Range Operators".

    sherm--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherm Pendley, Aug 21, 2008
    #5
  6. Sherm Pendley <> wrote in
    news::

    > cartercc <> writes:
    >
    >> Set a flag that by default is false, turns to true when it hits
    >> 'Endpoint:' and turns to false when it hits the next 'Beginpoint:'
    >> and print if $flag
    >>
    >> #!/usr/bin/perl
    >> use strict;
    >> my $flag = 0;
    >>
    >> open INFILE, "<test.txt" or die "Can't open: $!";
    >> while (<INFILE>)
    >> {
    >> if ($_ =~ /^Endpoint:/ and $flag == 0) {$flag = 1; }
    >> elsif ($_ =~ /Beginpoint:/ and $flag == 1) {$flag = 0; }
    >> print if $flag;
    >> }

    >
    > Why reinvent the wheel? Perl already has a built-in flip-flop operator
    > that does exactly what you describe:
    >
    > while (<INFILE>) {
    > print if ( /^Endpoint:/ .. /^Beginpoint:/ );
    > }


    While I do not like cartercc's proposed solution, using the range
    operator does result in lines beginning with Beginpoint: to be printed
    as well.

    If that is not desired, the body of the loop needs to be changed to:

    print if /^Endpoint:/ .. /^Beginpoint:/ and not /^Beginpoint:/;

    Of course, cartercc's solution can be stated more succinctly as:

    my $flag;

    while ( <DATA> ) {
    not $flag and /^Endpoint:/ and $flag = 1;
    $flag and /^Beginpoint:/ and $flag = 0;
    print if $flag;
    }

    As usual, beauty is in the eye of the beholder.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Aug 21, 2008
    #6
  7. Sherm Pendley, Aug 22, 2008
    #7
  8. Guest

    On Aug 21, 6:01 pm, Sherm Pendley <> wrote:
    > "A. Sinan Unur" <> writes:
    >
    > > As usual, beauty is in the eye of the beholder.

    >
    > I guess you never played D&D... Beholders are *ugly*! :)
    >
    > sherm--
    >
    > --
    > My blog:http://shermspace.blogspot.com
    > Cocoa programming in Perl:http://camelbones.sourceforge.net


    Hi All,

    Thank you every one replied and suggested solutions. I really
    appreciate it.

    I would like to print the whole "Endpoint" in one line, as an example:

    "Endpoint: core1_2/tx_lr/ifft128/u7_mem_fft_0/
    u5_data_ram_5/\mem_reg[8][13]/D (v) checked with leading edge of
    'x_clk' "

    instead of two lines.

    Can some one suggest how to do this.

    Thx
    SR
    , Aug 22, 2008
    #8
  9. cartercc Guest

    On Aug 22, 2:50 am, wrote:
    > I would like to print the whole "Endpoint" in one line, as an example:


    You might try removing the newline at the end of every line that
    matches /^Endpoint:/, perhaps by chomping it before printing it.

    CC
    cartercc, Aug 22, 2008
    #9
  10. [OT] Re: printing two consecutive lines

    Sherm Pendley <> wrote in news:m14p5d278u.fsf@dot-
    app.org:

    > "A. Sinan Unur" <> writes:
    >
    >> As usual, beauty is in the eye of the beholder.

    >
    > I guess you never played D&D... Beholders are *ugly*! :)


    Didn't know that. I had not even heard of the game until I was in my
    twenties. The game is not big in Turkey.

    Having seen

    http://www.wizards.com/dnd/images/leof_gallery/86716.jpg

    that phrase will have an entirely different meaning for the rest of my
    life.

    Thanks ;-)

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Aug 22, 2008
    #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. baptiste Auguié

    extracting values from consecutive lines

    baptiste Auguié, Oct 17, 2007, in forum: Ruby
    Replies:
    15
    Views:
    221
    7stud --
    Oct 18, 2007
  2. baptiste Auguié

    extract consecutive lines of data

    baptiste Auguié, Nov 24, 2007, in forum: Ruby
    Replies:
    8
    Views:
    121
    Robert Dober
    Nov 25, 2007
  3. Peter

    Finding consecutive lines

    Peter, Nov 5, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    198
    Michele Dondi
    Nov 6, 2004
  4. sbk
    Replies:
    5
    Views:
    145
  5. Ciba LO
    Replies:
    4
    Views:
    170
    Ciba LO
    Jul 8, 2006
Loading...

Share This Page