Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converting a RedNotebook backup file into a csv file which can be imported to Diarium
#!/bin/bash
: <<'COMMENT'
This script converts a text backup from RedNotebook into a csv file which can then be imported in Diarium.
Steps to do this:
1. export a backup from Rednotebook as plain text without marks
2. execute this script with the text file as argument
3. import the output csv with the migration feature in Diarium as a Daylio backup file
COMMENT
d1=$(date "+%s");
file=$1
f=${file%.*}
echo "" >> $1
mkdir export
echo "full_date,date,weekday,time,mood,activities,note_title,note" >> $f.csv
while read -r text; do
line=$(grep -n "===*" | cut -d: -f1)
echo -e "$line\n" >> found.txt
done < $file
sed -i '/^$/d' found.txt
lines=$(wc -l < found.txt)
lines2=$(wc -l < $file)
echo
echo "Found $lines entries in $lines2 lines of text"
echo
for l in $(seq 1 $lines); do
line1=$(cat found.txt | sed "$l!d")
line2=$(cat found.txt | sed "$(($l + 1))!d")
if [ -z $line2 ]; then
line2=$lines2
fi
date=$(cat $file | sed "$line1!d" | cut -d, -f2)
year=$(echo $date | cut -d. -f3)
month=$(echo $date | cut -d. -f2)
day=$(echo $date | cut -d. -f1)
for i in $(seq $(($line1 + 3)) $(($line2 - 1))); do
echo $(cat $file | sed "$i!d" | sed "s/\"/'/g") >> export/day$l.txt
done
echo $year-$month-$day,\"\",\"\",\"\",\"\",\"\",\"\",\""$(cat export/day$l.txt)"\" >> $f.csv
echo -ne "Parsed $l of $lines entries... \r"
done
d2=$(date "+%s");
duration=$((d2-d1));
echo "Parsed $l of $lines entries in $duration seconds"
echo
echo "Done. Output file is $f.csv"
echo
rm -r export/
rm found.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment