Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
William was kind enough to help me with the
following awk script to convert fields into vCal
standard items. It works beautifully in testing, but I
cannot get the program I use to output 24h time,
only AM and PM.I obviously need a statement to make AM disappear,
and PM to add 12 hrs to the number. Do William or
any awk gurus have suggestions?Here is the starting point:
Title of Event,05/30/2003,11:45 AM,15, 7
Title of Event2,05/30/2003, 9:00 AM,15, 1The result:
BEGIN:VCALENDAR
VERSION:1.0
BEGIN:VEVENT
SUMMARY:Title of event
DTSTART:20030530T114500
DTEND:20030530T120000
CATEGORIES:7
END:VEVENT
BEGIN:VEVENT
SUMMARY:Title of Event2
DTSTART:20030530T090000
DTEND:20030530T091500
CATEGORIES:1
END:VEVENT
END:VCALENDARThe script that works beautifully on military time:
#!/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 + $4if ( endminute >= 60 ) {
endhour += int(endminute / 60)
endminute = endminute % 60
}print "BEGIN:VEVENT"
print "SUMMARY:" $1
printf
"%s%s%02d%02d%s%02d%02d%02d\n","DTSTART:",
year,month,day, "T", hour,minute, "00"
printf "%s%s%02d%02d%s%02d%02d%02d\n",
"DTEND:", year,month,day, "T", endhour,endminute,
"00"
print "CATEGORIES:" $5
print "END:VEVENT"
}END {
print "END:VCALENDAR"
}Any thoughts on an awk function that I can add that
will use this field --> , 1:00 PM,
instead of --> , 13:00,Thanks in advance for any advice..

If there can sometimes be a space before the start time as in your example, it gets trickier. If instead the data will always be like
Title of Event,05/30/2003,11:45 AM,15, 7
Title of Event2,05/30/2003,9:00 AM,15, 1then this should work:
#!/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]
ampm=starttime[3]if ( ampm == "PM" )
hour += 12eventlength = $4
endhour = hour
endminute = minute + eventlengthif ( endminute >= 60 ) {
endhour += int(endminute / 60)
endminute %= 60
}print "BEGIN:VEVENT"
print "SUMMARY:" $1
printf "%s%s%02d%02d%s%02d%02d%s\n","DTSTART:", year, month, day, "T", hour, minute, "00"
printf "%s%s%02d%02d%s%02d%02d%s\n","DTEND:",year,month,day,"T",endhour,endminute, "00"
print "CATEGORIES:" $5
print "END:VEVENT"
}END {
print "END:VCALENDAR"
}

I'll check with our vendor, but unfortunately this
schedule program is very proprietary and does not
play well with others. I fully expect him to tell me
that it has to be output in this manner, as I know
there is a very strict control header, and when the
time is less than 4 digit, it puts the space in front. I
suspect this will make the script choke. Any way to
add an initial statement that strips out certain spaces
in certain fields?

Well, the situation is even stickier than I thought. My
vendor says that he has to output part of the title
field as:Doe, John
This is in the same field, but makes the script think
this is delimiting to the next field. I've asked him if
he can use a different delimiter, allowing us to tweak
the script. The time thing is set, unfortunately, and
will not left justify. any thoughts??

How about adding:
if ( index($3, " ") == 1 )
$3 = substr($3,2)right above the first "split()" in the revised script. That conditionally trims one blank off the beginning of the start time field.

For the problem of the first field potentially containing the delimiter character, it should be possible to have awk check upfront whether field #2 matches [number]/[number]/[number], and if it doesn't, and field #3 does, then perform a different set of assignments. It's not perfect, but at least the script would be making an intelligent guess. I don't have any time to try this out right now unfortunately. Good luck!

Thank you so much. I actually got that one solved by
making my software guy give me a file with a
delimiter of "^" instead of ",". I changed the
delimiter in your awk script. It works like a charm,
so nicely I can hardly stand it...I've got a new little problem to solve with the same
file, but I'll post it in a different message. awk may
not be the answer for this new issue, and I thank you
so much for helping me out with this one!!!!

As an afterthought,
sub(/^ */, "", $3)
is a neater way of stripping leading spaces off field $3 than
if ( index($3, " ") == 1 )
$3 = substr($3,2)As well as being less code, it copes with any number of spaces. It will probably make no difference in practice though.

![]() |
ftp script
|
function to compare chang...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |