Hi,
I desperate need to create a perl script which will search a directory of text files for a specific text pattern, and if found, then proceeds to insert a brand new line with custom text after a second, different text pattern further on down in the same file.
How far I've gotten: After searching forums and experimenting on my own (being a perl newbie) I was successfully able to cobble together a script which looks for what would be the 'second' text pattern and adds a new line with new text directly below that, however, I cannot figure out how to make that action conditional on finding the first text pattern. I have included this perl file at the end of the post.
The specifics of my situation is that I need to be able to modify an iCal .ics calendar files with a specific DESCRIPTION (in iCal: 'Note') such that an X-ATTR is added after the SUMMARY line, to mark the event as 'private'. This the very last piece of a workaround I have created in order to allow an iPhone write-access to CalDAV / iCal Server-hosted calendars (which it currently doesn't have, insanely.)
I am really hoping someone here can help --
Text patterns in question: First text pattern: ***Private-from-iPhone***
..and if found, looks for second text pattern: SUMMARY:
...then writes new line to insert/append after second text pattern: X-CALENDARSERVER-ACCESS:CONFIDENTIAL
A typical iCal event file that I would need to modify (listed with cat -v):How the file should look after script is run (the new X-CALENDARSERVER line is added after the SUMMARY line):BEGIN:VCALENDAR^M VERSION:2.0^M PRODID:-//Apple Inc.//iCal 3.0//EN^M CALSCALE:GREGORIAN^M BEGIN:VEVENT^M SEQUENCE:3^M DESCRIPTION:***Private-from-iPhone***^M UID:5749C919-23FB-4C97-BE1A-37DAB7921BE0^M TRANSP:OPAQUE^M DTSTART;TZID=US/Pacific:20090304T170000^M DTSTAMP:20090304T224810Z^M SUMMARY:test event^M CREATED:20090304T224749Z^M DTEND;TZID=US/Pacific:20090304T180000^M END:VEVENT^M END:VCALENDAR^M
My current perl script 'insline.pl', which only works to find the second text pattern and add a new line under it:BEGIN:VCALENDAR^M VERSION:2.0^M PRODID:-//Apple Inc.//iCal 3.0//EN^M CALSCALE:GREGORIAN^M BEGIN:VEVENT^M SEQUENCE:3^M DESCRIPTION:***Private-from-iPhone***^M UID:5749C919-23FB-4C97-BE1A-37DAB7921BE0^M TRANSP:OPAQUE^M DTSTART;TZID=US/Pacific:20090304T170000^M DTSTAMP:20090304T225151Z^M SUMMARY:test event^M X-CALENDARSERVER-ACCESS:CONFIDENTIAL^M CREATED:20090304T224749Z^M DTEND;TZID=US/Pacific:20090304T180000^M END:VEVENT^M END:VCALENDAR^M
Executed on the commandline as: perl -i insline.pl /..path-to-ical-calendar-directory/*.ics#!/usr/bin/perl use warnings; use strict; my ($debug); $debug = 0; $debug = 1; while (<>) { chomp; if (/SUMMARY:/) { s/$/\nX-CALENDARSERVER-ACCESS:CONFIDENTIAL^M/; } print "$_\n"; } print STDERR " ( Lines read: $. )\n"; exit(0);
I am running perl v5.8.8 on Mac OS Leopard 10.5.4.
One option might be change if (/SUMMARY:/) to if (/***Private-from-iPhone***/), then change the script to insert the X-CALENDARSERVER-ACCESS:CONFIDENTIAL line into a specific line number (line #13). This could work too, but of course I'm not sure how to do this either, and it would be safer to insert this line after SUMMARY line regardless of line number.
What should I do, o perl monks?
-dIn reply to help with conditional match? by ignatz99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |