Search code examples
bashawkicalendar

Convert ICS into table (awk)


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.


Solution

  • 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