Multi-line regex?

Multi-line regex?

am 27.08.2008 23:54:34 von STEVEN WHALEY

------_=_NextPart_001_01C9088F.7DE4D47D
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

First off, hello everyone.

=20

The program I'm trying to write requires me to grab a specific set of
multiline output from a command and store it in an array. I have output
that looks like this:

=20

Hosts associated with this datastore.

ag-vmsql01-srv.agbr.com

ag-vmsql01-srv.agbr.com

ag-vmsql02-srv.agbr.com

=20

Virtual machines on this datastore.

ag-tsgate-srv

ag-voicecon-srv

ag-ts01-srv

ag-testsql-srv

ag-sql0501-srv

ag-bartend-srv

=20

Templates on this datastore.

None

=20

Datastore Folder Structure.

Folder Path: '[Win_Prod_1]'

Files present

ag-bartend-srv

ag-ts01-srv

ag-voicecon-srv_1

ag-tsgate-srv

ag-sql0501-srv

ag-testsql-srv

=20

Folder Path: '[Win_Prod_1] ag-bartend-srv/'

Files present

ag-bartend-srv-flat.vmdk

ag-bartend-srv.nvram

ag-bartend-srv.vmdk

ag-bartend-srv.vmsd

ag-bartend-srv.vmx

ag-bartend-srv.vmxf

vmware.log

vmware-2.log

vmware-3.log

vmware-4.log

ag-bartend-srv-c17f6d6e.vswp

vmware-5.log

vmware-6.log

vmware-7.log

=20

Folder Path: '[Win_Prod_1] ag-ts01-srv/'

Files present

ag-ts01-srv.vmsd

ag-ts01-srv.vmx

ag-ts01-srv.vmxf

=20

=20

And I need to able to grab just one secition, like this:

=20

Folder Path: '[Win_Prod_1] ag-bartend-srv/'

Files present

ag-bartend-srv-flat.vmdk

ag-bartend-srv.nvram

ag-bartend-srv.vmdk

ag-bartend-srv.vmsd

ag-bartend-srv.vmx

ag-bartend-srv.vmxf

vmware.log

vmware-2.log

vmware-3.log

vmware-4.log

ag-bartend-srv-c17f6d6e.vswp

vmware-5.log

vmware-6.log

vmware-7.log

=20

and make each line an entry in an array. I do not actually need the
first two lines, but I can pop them off so grabbing them isn't a
problem. I can get a regex to match the "Folder Path:..." line but I'm
not sure how to get the rest of the section, up to the next empty line,
into an array. =20


Can anyone help? =20

=20

Thanks,

=20

Steven Whaley

=20

Systems Administrator

Associated Grocers, Inc.

225.769.2020x1426 Office

225.276.9981 Cell

=20


------_=_NextPart_001_01C9088F.7DE4D47D--

Re: Multi-line regex?

am 28.08.2008 00:15:07 von shawnhcorey

On Wed, 2008-08-27 at 16:54 -0500, STEVEN WHALEY wrote:
> and make each line an entry in an array. I do not actually need the
> first two lines, but I can pop them off so grabbing them isn't a
> problem. I can get a regex to match the "Folder Path:..." line but
> I'm
> not sure how to get the rest of the section, up to the next empty
> line,
> into an array.
>
>
> Can anyone help?

You can do this two ways: one with a multi-line regex; the other with a
Finite-State Automation (FSA).

With regex:

# Slurp everything in a scalar, say $contents
$contents =~ /Folder Path: '[Win_Prod_1] ag-bartend-srv\/'(.*?)Folder Path/ms;
$lines = $1;
@lines = split /\n/, $lines;

with FSA:

my $state = 0;
while( <> ){
chomp;
if( /Folder Path: '[Win_Prod_1] ag-bartend-srv\/'/ ){
$state = 1;
}elsif( /Folder Path/ ){
$state = 0;
}elsif( $state == 1 ){
push @lines, $_;
}
}

See `perldoc perlretut` and `perldoc perlre` for more information on
regular expressions.

--
Just my 0.00000002 million dollars worth,
Shawn

"Where there's duct tape, there's hope."
Cross Time Cafe

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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

Re: Multi-line regex?

am 28.08.2008 00:30:46 von jwkrahn

STEVEN WHALEY wrote:
> First off, hello everyone.

Hello,

> The program I'm trying to write requires me to grab a specific set of
> multiline output from a command and store it in an array. I have output
> that looks like this:
>
>
>
> Hosts associated with this datastore.
>
> ag-vmsql01-srv.agbr.com
>
> ag-vmsql01-srv.agbr.com
>
> ag-vmsql02-srv.agbr.com
>
>
>
> Virtual machines on this datastore.
>
> ag-tsgate-srv
>
> ag-voicecon-srv
>
> ag-ts01-srv
>
> ag-testsql-srv
>
> ag-sql0501-srv
>
> ag-bartend-srv
>
>
>
> Templates on this datastore.
>
> None
>
>
>
> Datastore Folder Structure.
>
> Folder Path: '[Win_Prod_1]'
>
> Files present
>
> ag-bartend-srv
>
> ag-ts01-srv
>
> ag-voicecon-srv_1
>
> ag-tsgate-srv
>
> ag-sql0501-srv
>
> ag-testsql-srv
>
>
>
> Folder Path: '[Win_Prod_1] ag-bartend-srv/'
>
> Files present
>
> ag-bartend-srv-flat.vmdk
>
> ag-bartend-srv.nvram
>
> ag-bartend-srv.vmdk
>
> ag-bartend-srv.vmsd
>
> ag-bartend-srv.vmx
>
> ag-bartend-srv.vmxf
>
> vmware.log
>
> vmware-2.log
>
> vmware-3.log
>
> vmware-4.log
>
> ag-bartend-srv-c17f6d6e.vswp
>
> vmware-5.log
>
> vmware-6.log
>
> vmware-7.log
>
>
>
> Folder Path: '[Win_Prod_1] ag-ts01-srv/'
>
> Files present
>
> ag-ts01-srv.vmsd
>
> ag-ts01-srv.vmx
>
> ag-ts01-srv.vmxf
>
>
>
>
>
> And I need to able to grab just one secition, like this:
>
>
>
> Folder Path: '[Win_Prod_1] ag-bartend-srv/'
>
> Files present
>
> ag-bartend-srv-flat.vmdk
>
> ag-bartend-srv.nvram
>
> ag-bartend-srv.vmdk
>
> ag-bartend-srv.vmsd
>
> ag-bartend-srv.vmx
>
> ag-bartend-srv.vmxf
>
> vmware.log
>
> vmware-2.log
>
> vmware-3.log
>
> vmware-4.log
>
> ag-bartend-srv-c17f6d6e.vswp
>
> vmware-5.log
>
> vmware-6.log
>
> vmware-7.log
>
>
>
> and make each line an entry in an array.

Is your data actually double spaced like that or is that an artifact of
the email software you are using? If your email software is modifying
the data could you please post the actual data (without the
quoted-printable encoding.) TIA



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

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

Re: Multi-line regex?

am 28.08.2008 00:46:12 von Rob Dixon

STEVEN WHALEY wrote:
>
> The program I'm trying to write requires me to grab a specific set of
> multiline output from a command and store it in an array. I have output
> that looks like this:
>
[lengthy data snipped]
>
> and make each line an entry in an array. I do not actually need the
> first two lines, but I can pop them off so grabbing them isn't a
> problem. I can get a regex to match the "Folder Path:..." line but I'm
> not sure how to get the rest of the section, up to the next empty line,
> into an array.

First of all, in case your data is currently in a single long string, lets say
$data, do this to it.

my @data = split /\n/, $data; # Split into one line per array element
s/^\s+//, s/\s+$// foreach @data; # Remove leading and trailing whitespace
@data = grep /\S/, @data; # Remove all blank lines

Now to extract part of @data into @section do this

my @section = grep(
(/Folder Path.*ag-bartend-srv/ ... /Folder Path/) =~ /^\d+$/,
@data,
);

which copies lines into @section starting at the first one containing 'Folder
Path' and 'ag-bartend-srv', and up but not including the next line that contains
'Folder Path'.

Then

print "$_\n" foreach @section;

to show what we've caught :)

HTH,

Rob



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

Re: Multi-line regex?

am 29.08.2008 02:06:56 von Rob Dixon

Mr. Shawn H. Corey wrote:
>
> You can do this two ways: one with a multi-line regex; the other with a
> Finite-State Automation (FSA).
>
> With regex:
>
> # Slurp everything in a scalar, say $contents
> $contents =~ /Folder Path: '[Win_Prod_1] ag-bartend-srv\/'(.*?)Folder Path/ms;
> $lines = $1;
> @lines = split /\n/, $lines;
>
> with FSA:
>
> my $state = 0;
> while( <> ){
> chomp;
> if( /Folder Path: '[Win_Prod_1] ag-bartend-srv\/'/ ){
> $state = 1;
> }elsif( /Folder Path/ ){
> $state = 0;
> }elsif( $state == 1 ){
> push @lines, $_;
> }
> }
>
> See `perldoc perlretut` and `perldoc perlre` for more information on
> regular expressions.

Hmm, a Finite State Automaton with two states? I thought that was called 'use a
flag'?

Rob

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

Re: Multi-line regex?

am 29.08.2008 02:28:30 von shawnhcorey

On Fri, 2008-08-29 at 01:06 +0100, Rob Dixon wrote:
> Hmm, a Finite State Automaton with two states? I thought that was called 'use a
> flag'?
>
> Rob
>

It is. It is also called a state machine.

BTW, two is finite :)


--
Just my 0.00000002 million dollars worth,
Shawn

"Where there's duct tape, there's hope."
Cross Time Cafe

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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

RE: Multi-line regex?

am 29.08.2008 21:36:29 von STEVEN WHALEY

Thanks for the help everyone. I was able to get it working. =20

Steven Whaley

Systems Administrator
Associated Grocers, Inc.
225.769.2020x1426 Office
225.276.9981 Cell

-----Original Message-----
From: Mr. Shawn H. Corey [mailto:shawnhcorey@magma.ca]=20
Sent: Thursday, August 28, 2008 7:29 PM
To: Rob Dixon
Cc: Perl Beginners
Subject: Re: Multi-line regex?

On Fri, 2008-08-29 at 01:06 +0100, Rob Dixon wrote:
> Hmm, a Finite State Automaton with two states? I thought that was
called 'use a
> flag'?
>=20
> Rob
>=20

It is. It is also called a state machine.

BTW, two is finite :)


--=20
Just my 0.00000002 million dollars worth,
Shawn

"Where there's duct tape, there's hope."
Cross Time Cafe

"Perl is the duct tape of the Internet."
Hassan Schroeder, Sun's first webmaster


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



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