Skip to content

Instantly share code, notes, and snippets.

@kuba-orlik
Last active July 29, 2022 10:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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
@tagMacher
Copy link

Thank you. This works great. I had just the ics file and no corresponding props file, so had to comment our corresponding backup command.

@codiflow
Copy link

codiflow commented Oct 12, 2021

Be careful with just copying and pasting this snippet. In line 30 there is a hardcoded 2016 which should be replaced by $YEAR.

It would also be better to surround all variables with curly brackets like ${VARIABLE_NAME}. Bash best practice: "Surround your variables with {}. Otherwise bash will try to access the $ENVIRONMENT_app variable in /srv/$ENVIRONMENT_app, whereas you probably intended /srv/${ENVIRONMENT}_app."

But then you would need to overhaul the whole script :-)

@gatrag
Copy link

gatrag commented Nov 1, 2021

Hi guys!

I need some help, don't know much about program or code, but i need to crop a ICS file, since I download the ENTIRE calendar for safety, and now google doesn't allow me to upload since it's 5 mb large, and only allows 1mb. Can anyone help me reducing it for me please? I've tried everything i could find (edit in excel, try to convert to csv and then crop but useless)... nothing works. It's my work's agenda so you can imagine how important this is for me and my team.

@kuba-orlik
Copy link
Author

kuba-orlik commented Nov 1, 2021 via email

@vxnick
Copy link

vxnick commented Jul 29, 2022

If you're using MacOS then you will need to brew install coreutils and change csplit on line 19 to gcsplit, otherwise you will see a csplit: *}: bad repetition count error.

Additionally, you will need to change sed on line 30 to gsed.

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