On 25-Apr-05, at 10:06 AM, Jay Savage wrote:
On 4/25/05, Kevin Horton <[EMAIL PROTECTED]> 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).
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.
-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.
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?
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
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>