forked from alejandroscf/bash-ical-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathical2txt
executable file
·60 lines (51 loc) · 1.62 KB
/
ical2txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Code from StackOverflow user Charles Duffy:
# https://stackoverflow.com/questions/37334681/parsing-ics-file-with-bash
# echo "Usage:"
# echo " $0"
# echo " Pipe in an ical file"
# echo ""
# echo " Output structure:"
# echo " starttime endtime text"
# echo " starttime and endtime are linux epochs"
local_date() {
local tz=${tzid[$1]}
local dt=${content[$1]}
if [[ $dt = *Z ]]; then
tz=UTC
dt=${dt%Z}
fi
shift
if [[ $dt = *T* ]]; then
dt="${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
else
dt="${dt:0:4}-${dt:4:2}-${dt:6:2}"
fi
# note that this requires GNU date
date --date="TZ=\"$tz\" $dt" "$@"
}
handle_event() {
#if [[ "${content[LAST-MODIFIED]}" = "${content[CREATED]}" ]]; then
# echo "New Event Created"
#else
# echo "Modified Event"
#fi
#printf '%s\t' "$(local_date DTSTART)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
duration=$(( $(local_date DTEND +%s) - $(local_date DTSTART +%s) ));
printf '%s\t' "$(local_date DTSTART +%s)" "$(local_date DTEND +%s)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
}
declare -A content=( ) # define an associative array (aka map, aka hash)
declare -A tzid=( ) # another associative array for timezone info
while IFS=: read -r key value; do
value=${value%$'\r'} # remove DOS newlines
if [[ $key = END && $value = VEVENT ]]; then
handle_event # defining this function is up to you; see suggestion below
content=( )
tzid=( )
else
if [[ $key = *";TZID="* ]]; then
tzid[${key%%";"*}]=${key##*";TZID="}
fi
content[${key%%";"*}]=$value
fi
done