For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > Query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Query
Sudhindra Bhat

2004-05-18, 3:30 am

Hi

Thanks. But there is a small issue. Considering the same example, the piece
of code sent by you prints 123456 which is not on the same line as "Test:"
But it doesn't print the characters 123456 ABCDEF which is on the same line
as "Test:"

Regards,
Sudhindra

-----Original Message-----
From: John W.Krahn [mailto:krahnj@acm.org]
Sent: Monday, May 17, 2004 4:56 PM
To: Perl Beginners
Subject: Re: Query

On Monday 17 May 2004 03:15, Sudhindra Bhat wrote:
>
> Hi


Hello,

> I wanted some help on a piece of code that I was writing. Well the
> requirement is like this. I have a file whose looks like this
>
> (1) Test: 123456 ABCDEF
>
> 123456
>
> (2) Results: ABCDEF
>
> Now I want my script to output all the contents between the two tags
> Test and Results. i.e. 123456 ABCDEF 123456. Can someone help me with
> this?



while ( <FILE> ) {
if ( /Test:/ .. /Results:/ and !/Test:/ and !/Results:/ ) {
print
}
}



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Sudhindra Bhat

2004-05-18, 8:34 am

Hi

This doesn't seem to work. I get a blank output. But yes the output =
that is
want is=20

123456 ABCDEF
123456

Regards,
Sudhindra

-----Original Message-----
From: Jose Alves de Castro [mailto:jcastro@telbit.pt]=20
Sent: Tuesday, May 18, 2004 3:55 PM
To: Sudhindra Bhat
Cc: Perl Beginners
Subject: Re: Query

On Tue, 2004-05-18 at 07:36, Sudhindra Bhat wrote:
> Hi
>=20
> Thanks. But there is a small issue. Considering the same example, the

piece
> of code sent by you prints 123456 which is not on the same line as =

"Test:"
> But it doesn't print the characters 123456 ABCDEF which is on the =

same
line
> as "Test:"=20


That's because it is *not* printing the line with "Test:"... it is only
printing the lines between the one that matches "Test:" and the one =
that
matches "Results:"

This prevents the line with "Test:" from being printed: !/Test:/

This prevents the line with "Results:" from being printed: !/Results:/

Exactly what output were you expecting?
Something like this, perhaps:

=3D=3D=3D
123456 ABCDEF

123456

(2) =20
=3D=3D=3D

Would that be it?

If so, try=20

while (<FILE> ) {
if ( /Test:/ .. /Results:/ ) {
if ( /Test:/ ) { print $' }
elsif ( /Results:/ ) { print $` }
else { print }
}
}

HTH,


jac

> Regards,
> Sudhindra
>=20
> -----Original Message-----
> From: John W.Krahn [mailto:krahnj@acm.org]=20
> Sent: Monday, May 17, 2004 4:56 PM
> To: Perl Beginners
> Subject: Re: Query
>=20
> On Monday 17 May 2004 03:15, Sudhindra Bhat wrote:
>=20
> Hello,
>=20
tags[color=darkred]
with[color=darkred]
>=20
>=20
> while ( <FILE> ) {
> if ( /Test:/ .. /Results:/ and !/Test:/ and !/Results:/ ) {
> print
> }
> }
>=20
>=20
>=20
> John
> --=20
> use Perl;
> program
> fulfillment
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>

--=20
Jos=E9 Alves de Castro <jcastro@telbit.pt>
Telbit - Tecnologias de Informa=E7=E3o
Sudhindra Bhat

2004-05-18, 9:31 am

Thanks a million. It works perfectly.

Regards,
Sudhindra

-----Original Message-----
From: Jose Alves de Castro [mailto:jcastro@telbit.pt]=20
Sent: Tuesday, May 18, 2004 5:01 PM
To: Sudhindra Bhat
Cc: Perl Beginners
Subject: RE: Query

In that case, with this:

#!/usr/bin/perl -w
use strict;

while (<> ) {
if ( /Test:/ .. /Results:/ and !/Results:/ ) {
if ( /Test:\s*/ ) {
print $'
}
else {
print if /./
}
}
}

you can get that output ( run the script with the input file).

HTH,

jac

On Tue, 2004-05-18 at 12:28, Sudhindra Bhat wrote:
> Hi
>=20
> This doesn't seem to work. I get a blank output. But yes the output =

that
is
> want is=20
>=20
> 123456 ABCDEF
> 123456
>=20
> Regards,
> Sudhindra
>=20
> -----Original Message-----
> From: Jose Alves de Castro [mailto:jcastro@telbit.pt]=20
> Sent: Tuesday, May 18, 2004 3:55 PM
> To: Sudhindra Bhat
> Cc: Perl Beginners
> Subject: Re: Query
>=20
> On Tue, 2004-05-18 at 07:36, Sudhindra Bhat wrote:
the[color=darkred]
> piece
"Test:"[color=darkred]
same[color=darkred]
> line
>=20
> That's because it is *not* printing the line with "Test:"... it is =

only
> printing the lines between the one that matches "Test:" and the one =

that
> matches "Results:"
>=20
> This prevents the line with "Test:" from being printed: !/Test:/
>=20
> This prevents the line with "Results:" from being printed: =

!/Results:/
>=20
> Exactly what output were you expecting?
> Something like this, perhaps:
>=20
> =3D=3D=3D
> 123456 ABCDEF
>=20
> 123456
>=20
> (2) =20
> =3D=3D=3D
>=20
> Would that be it?
>=20
> If so, try=20
>=20
> while (<FILE> ) {
> if ( /Test:/ .. /Results:/ ) {
> if ( /Test:/ ) { print $' }
> elsif ( /Results:/ ) { print $` }
> else { print }
> }
> }
>=20
> HTH,
>=20
>=20
> jac
>=20
the[color=darkred]
tags[color=darkred]
with[color=darkred]
> --=20
> Jos=E9 Alves de Castro <jcastro@telbit.pt>
> Telbit - Tecnologias de Informa=E7=E3o

--=20
Jos=E9 Alves de Castro <jcastro@telbit.pt>
Telbit - Tecnologias de Informa=E7=E3o


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

John W. Krahn

2004-05-18, 6:30 pm

Sudhindra Bhat wrote:
>
> Hi


Hello,

> This doesn't seem to work. I get a blank output. But yes the output that is
> want is
>
> 123456 ABCDEF
> 123456


while ( <FILE> ) {
next unless /\S/;
if ( s/^.*?Test:\s*// .. /Results:/ and !/Results:/ ) {
print
}
}



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com