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):
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
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: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

My current perl script 'insline.pl', which only works to find the second text pattern and add a new line under it:
#!/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);
Executed on the commandline as:  perl -i insline.pl /..path-to-ical-calendar-directory/*.ics

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?

-d

In reply to help with conditional match? by ignatz99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.