Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 827
rororo Avatar asked Sep 26 '26 06:09

rororo


2 Answers

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.

like image 103
Gerard H. Pille Avatar answered Sep 28 '26 22:09

Gerard H. Pille


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
like image 29
RomanPerekhrest Avatar answered Sep 28 '26 20:09

RomanPerekhrest