Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printing two consecutive lines

Reply
Thread Tools

printing two consecutive lines

 
 
icebrakers2008@gmail.com
Guest
Posts: n/a
 
      08-21-2008
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







 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      08-21-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      08-21-2008
(E-Mail Removed) wrote in
news:(E-Mail Removed):

> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      08-21-2008
On Aug 20, 8:08*pm, (E-Mail Removed) 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;
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      08-21-2008
cartercc <(E-Mail Removed)> 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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      08-21-2008
Sherm Pendley <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> cartercc <(E-Mail Removed)> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      08-22-2008
"A. Sinan Unur" <(E-Mail Removed)> 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
 
Reply With Quote
 
icebrakers2008@gmail.com
Guest
Posts: n/a
 
      08-22-2008
On Aug 21, 6:01*pm, Sherm Pendley <(E-Mail Removed)> wrote:
> "A. Sinan Unur" <(E-Mail Removed)> 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

 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      08-22-2008
On Aug 22, 2:50*am, (E-Mail Removed) 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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      08-22-2008
Sherm Pendley <(E-Mail Removed)> wrote in news:m14p5d278u.fsf@dot-
app.org:

> "A. Sinan Unur" <(E-Mail Removed)> 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/le...lery/86716.jpg

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

Thanks

Sinan

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

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extract consecutive lines of data baptiste Auguié Ruby 8 11-25-2007 03:46 PM
extracting values from consecutive lines baptiste Auguié Ruby 15 10-18-2007 10:12 PM
How to break the lines in printing to html lines? Ciba LO Perl Misc 4 07-08-2006 02:58 AM
search/replace for consecutive blank lines sbk Perl Misc 5 03-28-2005 04:31 AM
Finding consecutive lines Peter Perl Misc 7 11-06-2004 08:06 AM



Advertisments