Skip to content

Instantly share code, notes, and snippets.

@kuba-orlik
Last active July 3, 2024 20:04
Show Gist options
  • Save kuba-orlik/7345a22609962011f6a377e351e5574a to your computer and use it in GitHub Desktop.
Save kuba-orlik/7345a22609962011f6a377e351e5574a to your computer and use it in GitHub Desktop.
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
@ewaldbenes
Copy link

Thanks for the work but as @codiflow pointed out this script does not work as-is and should not be executed blindly.

Here is a reworked edition of this script which is save to use and avoids these pitfalls:
https://gist.github.com/ewaldbenes/0067b79250b4dc65591bc606325ce90e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment