On Sat, Dec 22, 2012 at 04:45:21PM +0530, punit jain wrote:
> Hi,
> 
> I have a file like below : -
> 
> BEGIN:VCARD
> VERSION:2.1
> EMAIL:te...@test.com
> FN:test1
> REV:20101116T030833Z
> UID:644938456.1419.
> END:VCARD
> 
> >From <>(S_______-000000000003) Tue Nov 16 03:10:15 2010
> content-class: urn:content-classes:person
> Date: Tue, 16 Nov 2010 11:10:15 +0800
> Subject: test
> Message-ID: <644938507.1420>
> MIME-Version: 1.0
> Content-Type: text/x-vcard; charset="utf-8"
> 
> BEGIN:VCARD
> VERSION:2.1
> EMAIL:te...@test.com
> FN:test2
> REV:20101116T031015Z
> UID:644938507.1420
> END:VCARD
> 
> 
> 
> My requirement is to get all text between BEGIN:VCARD and END:VCARD and all
> the instances. So o/p should be :-
> 
> BEGIN:VCARD
> VERSION:2.1
> EMAIL:te...@test.com
> FN:test1
> REV:20101116T030833Z
> UID:644938456.1419.
> END:VCARD
> 
> BEGIN:VCARD
> VERSION:2.1
> EMAIL:te...@test.com
> FN:test2
> REV:20101116T031015Z
> UID:644938507.1420
> END:VCARD
> 
> I am using below regex  :-
> 
> my $fh = IO::File->new("$file", "r");
> my $script = do { local $/; <$fh> };
>                                 close $fh;
>                                 if (
>                                    $script =~ m/
>                                         (^BEGIN:VCARD\s*(.*)
>                                         ^END:VCARD\s+)/sgmix
>                                 ){
>                                 print OUTFILE $1."\n";
>                                 }
> 
> However it just prints 1st instance and not all.

It also prints the text between the two instances, right?

> Any suggestions ?

You need a non greedy match .*? instead of the greedy match .* that you
are using.  Then you'll need to use while instead of if.

Or perhaps you'd prefer:

 $ perl -ne 'print if /BEGIN:VCARD/ .. /END:VCARD/' < in > out

or

 $ perl -n00e 'print if /^BEGIN:VCARD/' < in > out

See perldoc perlrun for the switches and "Range Operators" from perdoc
perlop for ..

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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


Reply via email to