Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active August 15, 2023 01:51
Show Gist options
  • Save ernstki/6d17ab342aa6a1a2de788b487e7fb29b to your computer and use it in GitHub Desktop.
Save ernstki/6d17ab342aa6a1a2de788b487e7fb29b to your computer and use it in GitHub Desktop.
Quick-and-dirty iCalendar .ics parser in AWK; extracts attendees only
#!/usr/bin/awk -f
##
## Parse ATTENDEEs from iCalendar (.ics) files
##
## Author: Kevin Ernst <ernstki -at- mail.uc.edu>
## Date: 26 July 2021
## Source: https://gist.github.com/ernstki/6d17ab342aa6a1a2de788b487e7fb29b
## License: WTFPL
##
## Hint: Pipe into `column -ts$'\t'` for a visually-aligned table
##
BEGIN {
FS = ";"; OFS = "\t"
# remove this test if you want to be able to pipe into this script
if (ARGC == 1) {
print "ERROR: Expecting a filename as first argument." > "/dev/stderr"
exit 1
}
print "email", "last_name", "first_name", "org"
}
{
if ($1 == "ATTENDEE") {
line = $0
getline
# space at the beginning means continued from previous line
gsub(/^ /, "")
$0 = sprintf("%s%s", line, $0)
# remove all CRs
gsub(/\r/,"")
# extract the full name from CN
split($4, cn, "\"")
split(cn[2], n, ", ")
last = n[1]
first = n[2]
# parse out the organization, if there is one
if (match(first, /\(.+\)/)) {
org = substr(first, RSTART, RLENGTH)
gsub(/[()]/, "", org)
gsub(/ \(.*\)/, "", first)
}
# remove middle initial from first name part
sub(/ [A-Z]\. ?/, "", first)
# extract the email from PARTSTAT (participant status)
split($5, ps, ":")
email = tolower(ps[3])
# print the results
print email, last, first, org
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment