You are not logged in.

#1 2009-05-29 22:41:20

Cippa Lippa
Member
From: Toronto, ON
Registered: 2007-04-12
Posts: 159

replacing multiple lines of text in a file

Hello

I have an interesting problem

I have a scheduling software which outputs a iCalendar file - this iCalendar file can be read by Kontact but all the events have NO alarm
I Checked the file and what I need to add is :

BEGIN:VALARM
DESCRIPTION:
ACTION:DISPLAY
TRIGGER;VALUE=DURATION:-PT15M
END:VALARM

before

END:VEVENT

problem is that
1) the scheduling software exports the file often - so any modification would be overwritten - which means I have to have the script run periodically
2) the script shouldn't run more than once on the file otherwise I would get multiple alarm tags...

what I though of doing is

check for

TZID:America/Toronto
END:VEVENT

and replace it with

TZID:America/Toronto
BEGIN:VALARM
DESCRIPTION:
ACTION:DISPLAY
TRIGGER;VALUE=DURATION:-PT15M
END:VALARM
END:VEVENT

which should prevent all problems.

Now the big problem is that I have not found a single way around to have a script doing this for me.
I have tried sed but it doesn't support multiple lines
I have tried awk which is able to insert multile lines but somehow cannot search for multiple lines
The added complication is that I have a lot of special characters in the text to search and replace

any of you good souls can help me with this???

Cippa

Offline

#2 2009-05-29 23:03:20

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: replacing multiple lines of text in a file

This should work:

#!/bin/bash

INSERT='BEGIN:VALARM\nDESCRIPTION:\nACTION:DISPLAY\nTRIGGER;VALUE=DURATION:-PT15M\nEND:VALARM'

sed -e "/TZID:America\/Toronto$/N;s/\(TZID:America\/Toronto\)\n\(END:VEVENT\)/\1\n$INSERT\n\2/" $1

I just learned from http://www.shell-fu.org/lister.php?id=539

Offline

Board footer

Powered by FluxBB