Computing.Net > Forums > Unix > help parsing data into a vCal item

help parsing data into a vCal item

Reply to Message Icon

Original Message
Name: bsteward
Date: June 5, 2003 at 14:58:18 Pacific
Subject: help parsing data into a vCal item
OS: OS X/BSD
CPU/Ram: G3-400mHz, 512 RAM
Comment:

I know enough here only to be dangerous, but need
some help. I have a commercial SCO Unix program
which is outputting data files with 4 fields, in .csv
format. I need to convert and parse this data into
standard vCal objects. I need advice on what tools to
use and how to script this, ideally so I can crontab it.
Any ideas? Anybody want to earn some cash doing
it?? Thanks in advance


Report Offensive Message For Removal

Response Number 1
Name: David Perry
Date: June 6, 2003 at 04:21:30 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

If you could show us an example of a vCal object should look like, someone would be glad to help you.


Report Offensive Follow Up For Removal

Response Number 2
Name: bsteward
Date: June 6, 2003 at 07:03:28 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

This is the starting point, a file in .csv exported
format...

Title of event,6/3/2003,8:30,30,category1
Title of event2,6/4/2003,9:00,60,category2

This is the end product, which is the vCal standard
object.......


BEGIN:VCALENDAR
VERSION:1.0
BEGIN:VEVENT
SUMMARY:Title of event
DTSTART:20030603T083000
DTEND:20030603T090000
CATEGORIES:Category1
END:VEVENT
BEGIN:VEVENT
SUMMARY:Title of Event2
DTSTART:20030604T090000
DTEND:20030604T100000
CATEGORIES:Category2
END:VEVENT
END:VCALENDAR


See why I want to automate that???


Report Offensive Follow Up For Removal

Response Number 3
Name: David Perry
Date: June 6, 2003 at 07:33:14 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

I'll look into it as I get some free time. This screams of awk so I would guess James Boothe, WilliamRobertson or nails will figure it out before I do as I'm not yet fluent with awk and these guys are pretty bright.


Report Offensive Follow Up For Removal

Response Number 4
Name: David Perry
Date: June 6, 2003 at 08:51:15 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

#!/bin/ksh
# set -x
if [ ! -f vcal.csv ] ; then
cat vcal.csv
Title of event,6/3/2003,8:30,30,category1
Title of event2,6/4/2003,9:00,60,category2
EOT
fi

# BEGIN:VCALENDAR
# VERSION:1.0
# BEGIN:VEVENT
# SUMMARY:Title of event
# DTSTART:20030603T083000
# DTEND:20030603T090000
# CATEGORIES:Category1
# END:VEVENT
# BEGIN:VEVENT
# SUMMARY:Title of Event2
# DTSTART:20030604T090000
# DTEND:20030604T100000
# CATEGORIES:Category2
# END:VEVENT
# END:VCALENDAR

echo "BEGIN:VCALENDAR" > vcal.txt
echo "VERSION:1.0" >> vcal.txt
IFS="," ; export IFS
cat vcal.csv | while read summary date time duration category ; do
typeset -Z2 hour=`echo $time | cut -f 1 -d ":"`
minute=`echo $time | cut -f 2 -d ":"`
typeset -Z2 month=`echo $date | cut -f 1 -d "/"`
typeset -Z2 day=`echo $date | cut -f 2 -d "/"`
year=`echo $date | cut -f 3 -d "/"`
DTSTART=${year}${month}${day}T${hour}${minute}

meeting_end_in_minutes=$(( ($hour * 60) + $minute + $duration))
((DTEND_MINUTE=$meeting_end_in_minutes%60))
typeset -Z2 DTEND_MINUTE=$DTEND_MINUTE
typeset -i DTEND_HOUR=$meeting_end_in_minutes/60
typeset -Z2 DTEND_HOUR=$DTEND_HOUR
DTEND=${year}${month}${day}T${DTEND_HOUR}${DTEND_MINUTE}

# echo $DTSTART
# echo $DTEND
echo "BEGIN:VEVENT" >> vcal.txt
echo "SUMMARY: $summary" >> vcal.txt
echo "DTSTART:$DTSTART" >> vcal.txt
echo "DTEND:$DTEND" >> vcal.txt
echo "CATEGORIES:$category" >> vcal.txt
echo "END:VEVENT" >> vcal.txt

done
echo "END:VCALENDAR" >> vcal.txt


Report Offensive Follow Up For Removal

Response Number 5
Name: David Perry
Date: June 6, 2003 at 08:53:01 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

This forum deleted the two less than signs between cat and vcal.txt in the first section.


Report Offensive Follow Up For Removal


Response Number 6
Name: bsteward
Date: June 6, 2003 at 12:38:59 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

OK...I'm officially humbled and amazed. I'll play with
this, but it's now clear to me why I don't earn my
living programming!! I'm completely impressed.
Thanks so much!! BTW: did you mean the two less
than signs between cat and vcal.csv?


Report Offensive Follow Up For Removal

Response Number 7
Name: David Perry
Date: June 6, 2003 at 13:00:12 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

Yes. sorry. For test purposes I was creating a two line vcal.csv file if it did not already exist.

It should have looked

cat less than less than EOT greater than vcal.csv

Feel free to disregard the first if ... fi block.

You're welcome and thank you.


Report Offensive Follow Up For Removal

Response Number 8
Name: WilliamRobertson
Date: June 8, 2003 at 09:43:09 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

> these guys are pretty bright
Thanks David! I showed that to my wife. Now she has to believe me.

Well you beat me to it, but how could I resist having a go anyway:

#############################################################
#!/usr/bin/awk -f

BEGIN {
FS = ","
OFS = ""
print "BEGIN:VCALENDAR\nVERSION:1.0"
}

{
split($2,startdate,"/")
month=startdate[1]
day=startdate[2]
year=startdate[3]

split($3,starttime,":")
hour=starttime[1]
minute=starttime[2]

endhour = hour
endminute = minute + $4

if ( endminute >= 60 ) {
endhour += int(endminute / 60)
endminute = endminute % 60
}

print "BEGIN:VEVENT"
print "SUMMARY:" $1
printf "%s%s%02d%02d%s%02d%02d\n","DTSTART:", year,month,day, "T", hour,minute
printf "%s%s%02d%02d%s%02d%02d\n", "DTEND:", year,month,day, "T", endhour,endminute
print "CATEGORIES:" $5
print "END:VEVENT"
}

END {
print "END:VCALENDAR"
}

This is a self-contained awk script, so you can run it as something like:

formatvcal.awk vcal.csv > vcal.txt



Report Offensive Follow Up For Removal

Response Number 9
Name: bsteward
Date: June 8, 2003 at 20:34:56 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

OK, guys..I'm WAY impressed. I'm so slow it's
frustrating me just trying to get these put into
executable forms, and you're spitting out code like
this. Forgive me, I'm just a lowly Mac geek who's
just started swimming in the Unix waters. But, I'm
learning...BTW: I'm really starting to be impressed
with this OS. Seriously powerful guts, but a nice GUI
when us wimps get scared.... Anyway, I honestly
have just started trying shell scripting. I'm going to
display my real wimpiness here... I understand we've
got a korn script in the first example, and an awk in
the second. The code actually makes pretty good
sense to me, but I can't even get them to execute. I
make a regular file with pico, then chmod +xr, right?
OS X/BSD is giving me command not found.....sorry
for being so lame....


Report Offensive Follow Up For Removal

Response Number 10
Name: David Perry
Date: June 9, 2003 at 02:57:02 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

You would get command not found for one of a couple of reasons.

1) the shell script is not in your path. Try executing it with ./scriptname or copying it to a directory that is in your path such as /usr/local/bin/

2) the first line of the script ( shell declaration line ) does not point to a valid shell. Feel free to change it to /bin/sh or the path to ksh if you have that installed.

The chmod command should look like

chmod ug+x scriptname

for a relative change to add execute privs for the owner and group or

chmod 755 scriptname to explicitly name the privs.

Enjoy the party. It is an OS with no predefined boundries.


Report Offensive Follow Up For Removal

Response Number 11
Name: WilliamRobertson
Date: June 9, 2003 at 03:59:53 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

"chmod +xr" should work fine, though the full syntax David gives is more explicit about what you want. You can also set absolute permissions using the symbolic syntax, which I like as I can never remember what all the numbers mean. e.g:

chmod u=rwx,g=rx,o= myscript.awk

You can edit your PATH variable in your .profile, so that it contains . or $HOME or $HOME/scripts or whatever (I have a "scripts" directory for useful stuff). Put local directories at the end so system ones are searched first.

In OSX the default shell is /bin/tcsh for some godknowswhy reason. I would strongly advise against getting used to the C shell. I actually downloaded an OSX Korn shell, though later on I discovered that the supplied /bin/zsh does pretty much the same job. You could even create /bin/ksh as a symbolic link for /bin/zsh so that scripts with #!/bin/ksh in their headers will run.

sudo ln /bin/zsh /bin/ksh
(supply your own password when prompted)

You can change your default shell in the Netinfo Manager application (in /Applications/Utilities).

> Enjoy the party
I'll second that. OSX is just awesome. I never thought I would be so impressed with an operating system.


Report Offensive Follow Up For Removal

Response Number 12
Name: bsteward
Date: June 9, 2003 at 07:17:06 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

I'll shut up and stop gushing, I promise, but can't thank
you guys enough for the help. I'm now inspired to go
over to the Mac forum to try and spread some of the
kindness you guys have shown me.

Anyway, I don't seem to have /bin/ksh, you're right. As
far as the other, am I going to cause problems if I
change my default shell to zsh? I can't find a
explanation for Apple's choice of tsch either....


Report Offensive Follow Up For Removal

Response Number 13
Name: David Perry
Date: June 9, 2003 at 09:28:38 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

the tcsh shell has a keyboard memory and tab completion which makes it nice for a working environment but it works poorly for scripting. You could just point the first line of scripts to something close to ksh and all should be well without affecting your environment.


Report Offensive Follow Up For Removal

Response Number 14
Name: WilliamRobertson
Date: June 9, 2003 at 10:45:47 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

You can of course use the default shell as your working environment (some people claim to like it) and just use ksh/zsh within scripts, but I have to admit I took one look at the csh prompt and went off to find something else. Whatever you set it to won't affect system behaviour, just your Terminal environment, so you're safe to experiment all you like.

Since I actually run ksh (downloaded from http://www.research.att.com/~gsf/download/tgz/ksh.2003-04-22.darwin.ppc.gz I think, though it was a while ago) I'm not sure what zsh is actually like as an interactive shell - I've only used it within scripts. From the man pages it seems to have a lot of powerful features, which I think I'm going to have to start trying out.



Report Offensive Follow Up For Removal

Response Number 15
Name: bsteward
Date: June 10, 2003 at 16:24:18 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

I'm excited....I'm actually getting this unix thing to
start agreeing with me, thanks to you guys. I'm still
having some difficulty getting it massaged into
exactly the right format.

I wasn't able to get any script to actually run in a
shell, so the awk script is where I'm at. It works
great, but I still have one little snag to make it right.

My output is this: DTSTART:20030603T0830

What I need is the "seconds" field filled, which would
give me: DTSTART:20030603T083000

As it's always "00", my brilliant solution was to add a
, "00" to the end of your statement

printf "%s%s%02d%02d%s%02d%02d\n","DTSTART:",
year,month,day, "T", hour,minute

However brilliant, I'm not getting a change. :)

Suggestions??

PS: I'm still working on the ksh shell, too, for my
own benefit.


Report Offensive Follow Up For Removal

Response Number 16
Name: bsteward
Date: June 10, 2003 at 17:26:51 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

Never mind...it works!! Even a blind squirrel finds
the nut sometimes. Added the parameter at the
beginning of the printf statement, another "%02d".
Works great!!

Now, yet another question. How do I install that ksh?
I downloaded it, but can't seem to get it moved to
/bin... Do I need to do more?


Report Offensive Follow Up For Removal

Response Number 17
Name: WilliamRobertson
Date: June 11, 2003 at 11:54:53 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

> Added the parameter at the beginning of the printf statement

That should be the end (rightmost).

Funny, I had a brief go with that ksh download the other day and couldn't even get it unzip. I'm sure it is where I got my copy from as I followed links from www.kornshell.com via AT&T - this is the official David Korn-endorsed ksh. I should probably have another go as it will be a newer version. On the other hand, zsh looks cool.

If you can expand it to an executable e.g. "pdksh", you then need to enter:

sudo mv pdksh /bin/ksh
(supply your own password when prompted)

sudo chmod ugo+x /bin/ksh

You may also need to append "/bin/ksh" to /etc/shells:

sudo pico /etc/shells

Make sure it works:
/bin/ksh
print Hello from ksh



Report Offensive Follow Up For Removal

Response Number 18
Name: bsteward
Date: June 11, 2003 at 18:28:22 Pacific
Subject: help parsing data into a vCal item
Reply: (edit)

I misspoke about where I put the addition to the
printf statement, and it does work beautifully now.
I've also been snooping around about the zsh script,
and it seems it includes everything ksh does, but
adds a few bells and whistles that guys like you and
David can use. (I'm still working on the basics!) One
thing I've noticed. In my /bin directory, I've got zsh,
as well as zsh-4.0.4. Not sure what that's all about,
they both have identical permissions, mod dates and
sizes.


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: help parsing data into a vCal item

Comments:

 


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