Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

one-liner multi-line regex problem
I'm trying to write a perl one-liner that will edit an iCalendar
format file to remove To Do items.  The file contains several
thousand lines, and I need to remove several multi-line blocks.  The
blocks to remove start with a line "BEGIN:VTODO" (without the quotes)
and end with a line "END:VTODO" (also without quotes).

I've tried the following one-liner,

perl -p -i.bak -e 's/BEGIN:VTODO.*END:VTODO//sg' file_name_to_edit

The .bak file is created, which tells me the one-liner is finding my
file, but the file is identical to the old one - i.e. the regex
doesn't seem to be matching anything.

I'm also wondering whether my proposed one-liner (if it worked) would
be too greedy.  Would it pull out everything between the first
BEGIN:VTODO and the last END:VTODO?

I'd appreciate any hints.

Thanks,

Kevin Horton

Report this thread to moderator Post Follow-up to this message
Old Post
Kevin Horton
04-25-05 08:56 PM


Re: one-liner multi-line regex problem
Hi Kevin

just hints, no solution :-)

Am Montag, 25. April 2005 12.59 schrieb Kevin Horton:
> I'm trying to write a perl one-liner that will edit an iCalendar
> format file to remove To Do items.  The file contains several
> thousand lines, and I need to remove several multi-line blocks.  The
> blocks to remove start with a line "BEGIN:VTODO" (without the quotes)
> and end with a line "END:VTODO" (also without quotes).
>
> I've tried the following one-liner,
>
> perl -p -i.bak -e 's/BEGIN:VTODO.*END:VTODO//sg' file_name_to_edit

according to perldoc perlrun, -p reads _one_ line after the other, so you
can't search for multiline patterns this way.

> The .bak file is created, which tells me the one-liner is finding my
> file, but the file is identical to the old one - i.e. the regex
> doesn't seem to be matching anything.
>
> I'm also wondering whether my proposed one-liner (if it worked) would
> be too greedy.

yes or no, depends from the working implementation :-)

> Would it pull out everything between the first
> BEGIN:VTODO and the last END:VTODO?

yes, if you try to match a string with the whole file in it with the regex
above.

>
> I'd appreciate any hints.
>
> Thanks,
>
> Kevin Horton

Report this thread to moderator Post Follow-up to this message
Old Post
John Doe
04-25-05 08:56 PM


Re: one-liner multi-line regex problem
On 4/25/05, Kevin Horton <khorton01@rogers.com> wrote:
> I'm trying to write a perl one-liner that will edit an iCalendar
> format file to remove To Do items.  The file contains several
> thousand lines, and I need to remove several multi-line blocks.  The
> blocks to remove start with a line "BEGIN:VTODO" (without the quotes)
> and end with a line "END:VTODO" (also without quotes).
>=20
> I've tried the following one-liner,
>=20
> perl -p -i.bak -e 's/BEGIN:VTODO.*END:VTODO//sg' file_name_to_edit
>=20
> The .bak file is created, which tells me the one-liner is finding my
> file, but the file is identical to the old one - i.e. the regex
> doesn't seem to be matching anything.


-p causes the file to be read one line at a time, which negates the
usefulness of /s.  If you have sufficient RAM to read the entire file
into memory, you can use the -0 option to "slurp" the file:

perl -0777 -p -i.bak -e 's/BEGIN:VTODO.*?END:VTODO//sg'

see perldoc perlrun for details
>=20
> I'm also wondering whether my proposed one-liner (if it worked) would
> be too greedy.  Would it pull out everything between the first
> BEGIN:VTODO and the last END:VTODO?
>=20

Yes it will.


HTH,

--jay

Report this thread to moderator Post Follow-up to this message
Old Post
Jay Savage
04-25-05 08:56 PM


Re: one-liner multi-line regex problem
> I'm trying to write a perl one-liner that will edit an iCalendar
> format file to remove To Do items.  The file contains several
> thousand lines, and I need to remove several multi-line blocks.  The
> blocks to remove start with a line "BEGIN:VTODO" (without the quotes)
> and end with a line "END:VTODO" (also without quotes).
>=20
> I've tried the following one-liner,
>=20
> perl -p -i.bak -e 's/BEGIN:VTODO.*END:VTODO//sg' file_name_to_edit

Assuming you have enough disk space:

perl -ane 'print unless /^BEGIN:VTODO/ .. /^END:VTODO/' old > new

perldoc perlrun for more info on perl's command line paramaters

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Gray
04-25-05 08:56 PM


Re: one-liner multi-line regex problem
On 25-Apr-05, at 10:06 AM, Jay Savage wrote:

> On 4/25/05, Kevin Horton <khorton01@rogers.com> wrote: 
>
>
> -p causes the file to be read one line at a time, which negates the
> usefulness of /s.  If you have sufficient RAM to read the entire file
> into memory, you can use the -0 option to "slurp" the file:
>
>    perl -0777 -p -i.bak -e 's/BEGIN:VTODO.*?END:VTODO//sg'

This seems to work perfectly.  I've studied the output for five
minutes, and can't find a problem.

Thank you very much.
>
> see perldoc perlrun for details

I've learned a lot in the last few minutes, now that I know which of
the perldoc files to look in. 
>
> Yes it will.

I looked at trying to use the "?" to stop the potential greedyness, but
I didn't grok how it worked.  Now that I have an example, I think I
understand it (again, as I thought I understood when I was first
puzzling through perl on vacation in Christmas 2003).  Hopefully my
understanding this time is more lasting. :)

Thanks so much to the several people who responded.

Kevin Horton
Ottawa, Canada
RV-8 - Finishing Kit
http://www.kilohotel.com/rv8


Report this thread to moderator Post Follow-up to this message
Old Post
Kevin Horton
04-26-05 08:56 AM


Re: one-liner multi-line regex problem
On 25-Apr-05, at 10:06 AM, Jay Savage wrote:

> On 4/25/05, Kevin Horton <khorton01@rogers.com> wrote: 
>
>
> -p causes the file to be read one line at a time, which negates the
> usefulness of /s.  If you have sufficient RAM to read the entire file
> into memory, you can use the -0 option to "slurp" the file:
>
>    perl -0777 -p -i.bak -e 's/BEGIN:VTODO.*?END:VTODO//sg'

This seems to work perfectly.  I've studied the output for five
minutes, and can't find a problem.

Thank you very much.
>
> see perldoc perlrun for details

I've learned a lot in the last few minutes, now that I know which of
the perldoc files to look in. 
>
> Yes it will.

I looked at trying to use the "?" to stop the potential greedyness, but
I didn't grok how it worked.  Now that I have an example, I think I
understand it (again, as I thought I understood when I was first
puzzling through perl on vacation in Christmas 2003).  Hopefully my
understanding this time is more lasting. :)

Thanks so much to the several people who responded.

Kevin Horton
Ottawa, Canada
RV-8 - Finishing Kit
http://www.kilohotel.com/rv8


Report this thread to moderator Post Follow-up to this message
Old Post
Kevin Horton
04-26-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.