Computing.Net > Forums > Unix > sed and shell script question

sed and shell script question

Reply to Message Icon

Original Message
Name: d3
Date: January 12, 2003 at 17:23:39 Pacific
Subject: sed and shell script question
OS: MAC OSX
CPU/Ram: 1G/400
Comment:

Hi All,
I'm trying to combine ical ( mac calendar) files in
to one giant file for all users.
What I need to do is:

first find the name of a calendar which is the file
name with a .ics extension eg “david.ics”
or it can be found on the 4th line of the file: X-WR-
CALNAME;VALUE=TEXT:david

2nd step is to then replace any where in the ics
file it says: SUMMARY: New Event

to SUMMARY: CalendarName- New Event

eg SUMMARY: go to post office would become:
SUMMARY: Work_calendar- go to post office

Repeate this for each file in the directoy then
make my own master calendar which would get
all the other calendars into one by using either
BEGIN:VEVENT and END:VEVENT of each
calandar and loop though or I could use head and
tail to ignore the first 6 and the last line. then paste
each calendars body in to one large calendar
which has its own header and lastline.

Heres an example file

BEGIN:VCALENDAR
CALSCALE:GREGORIAN
PRODID:-//Apple Computer\, Inc//iCal 1.0//EN
X-WR-CALNAME;VALUE=TEXT:Work_calendar
X-WR-TIMEZONE;VALUE=TEXT:Australia/
Sydney
VERSION:2.0
BEGIN:VEVENT
UID:DA53ED08-268C-11D7-8488-
000393A99196
SEQUENCE:1
DTSTART;TZID=Australia/
Sydney:20030114T141500
DTSTAMP:20030113T002043Z
SUMMARY: New Event
DURATION:PT1H15M
END:VEVENT
BEGIN:VEVENT
SUMMARY: go to post office
SEQUENCE:2
DTSTART;TZID=Australia/
Sydney:20030113T143000
DTSTAMP:20030109T232142Z
UID:E698B5BC-2428-11D7-BF13-
000393A99196
DURATION:PT1H
END:VEVENT
END:VCALENDAR

Thanks in advance



Report Offensive Message For Removal

Response Number 1
Name: d3
Date: January 12, 2003 at 23:56:14 Pacific
Subject: sed and shell script question
Reply: (edit)

I know I can use

cut -f 2 -d ':' filename |head -4|tail -1

to get the calendar name but I want some thing
more dynamic and i don't know how to loop
though each file.


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: January 13, 2003 at 07:59:31 Pacific
Subject: sed and shell script question
Reply: (edit)

Following script should do what you want:

#!/bin/sh

mv master.ics master.old
cp master.heading master.new
if [ -f master.ics ] ; then
___print "Could not remove master.ics - abort"
___exit 1
fi

awk '{\
if (substr($0,1,15)=="BEGIN:VCALENDAR")
___{getline
____getline
____getline
____getline
____getline
____next}
if (substr($0,1,13)=="END:VCALENDAR")
___next
if ($1!="SUMMARY:")
___print
else
__{fname=substr(FILENAME,1,index(FILENAME,".")-1)
___print "SUMMARY: " fname "- " substr($0,10)}
}
END {
print "END:VCALENDAR"
}' *.ics >> master.new

mv master.new master.ics
print "\nNew master calendar is master.ics\n"

exit 0

NOTES:

After copy/paste, the script should be exactly 582 characters.

To prevent the web page from losing my indentation, I changed leading spaces to underscores. You can change those back with:

sed "s/_/ /g" script > goodscript

The script starts a new master.ics with a header file. These header lines will be easier to maintain in master.heading rather than hardcoded in the awk script.

The awk program then processes all files in the current directory with ics suffix. The new master.ics is created with .new suffix so that it will not be included as input.

It is assumed that each calendar has exactly 6 header lines to be bypassed. This code could be changed to bypass a variable number of header lines until it reaches the last one, which is currently the VERSION line.

Each summary line is modified by inserting the name of the current FILENAME being processed, minus the .ics suffix.


Report Offensive Follow Up For Removal

Response Number 3
Name: d3
Date: January 13, 2003 at 15:08:18 Pacific
Subject: sed and shell script question
Reply: (edit)

Thanks, its greatly appreciated.
I had to change the "print" to "echo" in the shell
script and it worked fine, apart from inserting the
current file after the "Summary:" so I did that in sed
first (because I have no idea of AWK). And IT
WORKS even if it is a bit of a HACK.

Heres what i ended up with below.
Thanks again James

#!/bin/sh

rm_*.tmp
mv_master.ics_master.old
cp_master.heading
_master.new
if_[_-f_master.ics_]_;
_then
___echo_"Could_not_remove_master.ics_-_abort"
___exit_1
fi

ls_-1_*.ics_>_tmp
cut_-f_1_-d_'.'_tmp_>_tmp2

while_read_-r_line
do

________sed_-e_s/SUMMARY:/SUMMARY:$line:-'_'g_$line.ics_>_$line.tmp

done_>_master.new

mv_master.new_master.ics
echo_"New_master_calendar_is_master.ics"
rm *.tmp
exit_0


Report Offensive Follow Up For Removal

Response Number 4
Name: d3
Date: January 13, 2003 at 15:12:53 Pacific
Subject: sed and shell script question
Reply: (edit)

this code hopefully looks better
#!/bin/sh
rm_*.tmp
mv_master.ics_master.old
cp_master.heading_master.new
if_[_-f_master.ics_]_;_then
___echo_"Could_not_remove_master.ics_-
_abort"
___exit_1
fi
ls_-1_*.ics_>_tmp
cut_-f_1_-d_'.'_tmp_>_tmp2
while_read_-r_line
do
________sed_-e_s/SUMMARY:/
SUMMARY:$line:-'_'/g_$line.ics_>_$line.tmp

done_>_master.new

mv_master.new_master.ics
echo_"New_master_calendar_is_master.ics"

exit_0


Report Offensive Follow Up For Removal

Response Number 5
Name: WilliamRobertson
Date: January 30, 2003 at 15:42:46 Pacific
Subject: sed and shell script question
Reply: (edit)

> I had to change the "print" to "echo"

"print" is part of the Korn shell. It has more options than echo e.g. "print -u2" for error output and "print -p" for writing to a co-process. I use all Korn shell syntax and only fall back on the historical sh syntax if I really have to. I use it on my iMac even though OS 10.x came with csh as the default.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed and shell script question

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software