I am trying to parse an ics file into a table using awk.
My ics file looks like this (only posting one event here):
BEGIN:VEVENT
DTSTART:20171004T173000Z
DTEND:20171004T183000Z
DTSTAMP:20180209T144026Z
UID:[email protected]
CREATED:20171004T171653Z
DESCRIPTION:
LAST-MODIFIED:20171004T173916Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:This text is the summary
TRANSP:OPAQUE
X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
X-WR-ALARMUID:282393849382
UID:883928394839283948392
ACKNOWLEDGED:20171004T173915Z
X-APPLE-DEFAULT-ALARM:TRUE
END:VALARM
END:VEVENT
I am only interested in the lines starting with DTSTART, DTEND, CREATED, SUMMARY.
I came up with the following code
BEGIN{OFS="\t"}
$1=="DTSTART"{DTSTART=$2}
$1=="DTEND"{DTEND=$2}
$1=="CREATED"{CREATED=$2}
$1=="SUMMARY"{SUMMARY=$2}
{print DTSTART DTEND CREATED SUMMARY}
that I am executing with
awk -F":" -f ics.awk file.ics
but there seems to be an error somewhere. Plus, it would be nice to print a table header.
Just replace the last line with
/^END:VEVENT/ {print DTSTART, DTEND, CREATED, SUMMARY}
and the first by
BEGIN{
OFS="\t"
print "DTSTART", "DTEND", "CREATED", "SUMMARY"
}
Result:
DTSTART DTEND CREATED SUMMARY
20171004T173000Z 20171004T183000Z 20171004T171653Z This text is the summary
You were executing "{print DTSTART DTEND CREATED SUMMARY}" for every line.
Short awk solution (for static input file format):
awk -F':' '$1~/^(DTSTART|DTEND|CREATED|SUMMARY)/{
printf "%s%s",$2,($1=="SUMMARY"? ORS:"\t")
}' file.ics
The output:
20171004T173000Z 20171004T183000Z 20171004T171653Z This text is the summary
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With