printing two consecutive lines

I

icebrakers2008

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
 
S

Sherm Pendley

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--
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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 <[email protected]>
(remove .invalid and reverse each component for email address)

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

cartercc

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;
 
S

Sherm Pendley

cartercc said:
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--
 
A

A. Sinan Unur

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 <[email protected]>
(remove .invalid and reverse each component for email address)

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

icebrakers2008

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

sherm--

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
 
C

cartercc

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
 
A

A. Sinan Unur

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 <[email protected]>
(remove .invalid and reverse each component for email address)

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top