Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove old events from a big ics calendar file

Removes all events preceeding a given year from an .ics calendar file (the kind you can find e.g. in a Radicale-based CalDAV server).

Usage:

./trim-calendar.sh Personal 2016 # removes all events starting before the year 2016 from the calendar stored in Personal.ics file
#!/bin/bash
# first argument - the calendar file
# second argument - the year.
# All of the events preceeding the year will be removed from the calendar.
CALENDAR_FILE=$1
YEAR=$2
temp_dir=___temp
temp_files_digits=5
# backup the calendar just in case
cp $CALENDAR_FILE $CALENDAR_FILE.bak
cp $CALENDAR_FILE.props $CALENDAR_FILE.bak.props
# split the calendar file into separate files for each event. This script creates multiple files in the form of xx00000
total_files=`csplit -n $temp_files_digits $CALENDAR_FILE '/BEGIN:VEVENT/' {*} | wc -l`
echo "total events found: " $total_files
mkdir -p $temp_dir
# move the events to a separate temp directory
mv xx* $temp_dir
cd $temp_dir
grep -R DTSTART . | sed -s "s/\.\/\(xx[0-9]\+\).*:\([0-9]\{4\}\).*/\1 \2/g" | awk '{if($2>=2016) print $1;}' | xargs -L 1 cat > events
cat xx00000 events xx`printf "%05d" $((total_files-1))` > $CALENDAR_FILE
cd ../
rm $CALENDAR_FILE
mv $temp_dir/$CALENDAR_FILE $CALENDAR_FILE
rm -rf $temp_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.