Computing.Net > Forums > Unix > tweaking William's awk script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

tweaking William's awk script

Reply to Message Icon

Name: bsteward
Date: June 17, 2003 at 10:21:12 Pacific
OS: OS X 10.2.4
CPU/Ram: G3/500mHz
Comment:

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, 1

The 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:VCALENDAR

The 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 + $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%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..




Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: June 17, 2003 at 10:53:14 Pacific
Reply:

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, 1

then 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 += 12

eventlength = $4
endhour = hour
endminute = minute + eventlength

if ( 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"
}


0

Response Number 2
Name: bsteward
Date: June 17, 2003 at 11:11:04 Pacific
Reply:

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?


0

Response Number 3
Name: bsteward
Date: June 17, 2003 at 13:06:26 Pacific
Reply:

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??


0

Response Number 4
Name: WilliamRobertson
Date: June 17, 2003 at 13:17:52 Pacific
Reply:

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.


0

Response Number 5
Name: WilliamRobertson
Date: June 17, 2003 at 15:04:51 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: bsteward
Date: June 17, 2003 at 18:29:08 Pacific
Reply:

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!!!!


0

Response Number 7
Name: WilliamRobertson
Date: June 25, 2003 at 05:46:48 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

ftp script function to compare chang...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: tweaking William's awk script

awk script www.computing.net/answers/unix/awk-script/7067.html

Question about AWK script www.computing.net/answers/unix/question-about-awk-script/4590.html

pass 2 input files to awk script www.computing.net/answers/unix/pass-2-input-files-to-awk-script/4448.html