#! /bin/bash # This applications takes an text input file $FILE # and converts it into a gnucash account scheme with a random name. # The input file is read line by line where each line # should be formatted as # number name (no leading spaces) # or # category name (one leading space) # . # Empty lines are not allowed, number is expected to have 4 digits. # The output is a two layer structure # of all accounts. Whenever the first # digit of number changes, a new source group is created. # Whenever a new category line is passed, all following # accounts are put below. # # Example: # input: # 1234 A # 2000 B # Type B1 # 2001 B1a # 2002 B1b # Type B2 # 2010 B2a # 2011 B2b # output: # Class 0 # Class 1 # \- 1234 A # Class 2 # \- 2000 B # \- Type B1 # \- 2001 B1a # \- 2002 B1b # \- Type B2 # \- 2010 B2a # \- 2011 B2b # # (C) 2009, Michael Braun, michael-developer@fami-braun.de # Distributed under GPL v3.0 or newer. # see http://www.gnu.org/licenses/gpl.html for details echo "start" FILE="input.txt" if [ ! -f "$FILE" ]; then echo "no such file $FILE" exit 1; fi getuid() { dd if=/dev/urandom bs=1 count=32 2>/dev/null | md5sum | awk -F' ' '{print $1}'; } OUT="$(getuid).gnucash" ROOT=$(getuid); NUM=$(cat $FILE | wc -l) NUM=$(expr $NUM + 1); ID=0 echo -n > $OUT; cat >> "$OUT" < $NUM Root Account $ROOT ROOT 0 EOF LAST=0 CURR=$(getuid); CURRSUB=$CURR; addkontenklasse() { echo echo -n "add kontenklasse $LAST"; cat >>"$OUT" < Kontenklasse $LAST $CURR ASSET ISO4217 EUR 100 Erträge placeholder true $ROOT EOF echo " done"; } addkontenklasse; addkontengruppe() { echo echo -n "add kontengruppe $1"; cat >>"$OUT" < $ID $1 $CURRSUB ASSET ISO4217 EUR 100 $1 placeholder true $CURR EOF echo " done"; } addkonto() { cat >>"$OUT" < $1 $2 $(getuid) ASSET ISO4217 EUR 100 $2 $1 $CURRSUB EOF } while (true); do LL=$REPLY read if [ "x$REPLY" = "x" ]; then echo echo "end of file after $LL"; break; fi REPLY=$(echo "$REPLY" | sed 's/\s\+/ /g'); KTOID=$(echo "$REPLY" | sed 's/ .*//'); NAME=$(echo "$REPLY" | sed 's/^[0-9]\+ \+//'); if [ -z "$NAME" ]; then NAME="dummy"; fi #echo "$REPLY => $KTOID | $NAME"; echo -n "$KTOID " if [ -z "$KTOID" ]; then CURRSUB=$(getuid); ID=$(expr $ID + 1); addkontengruppe "$NAME"; else if [ $KTOID -gt "$LAST"999 ]; then # new cat LAST=$(echo $KTOID | sed 's/\(.\).*/\1/'); CURR=$(getuid); CURRSUB=$CURR; ID=0 addkontenklasse; fi addkonto "$KTOID" "$NAME"; fi done < "$FILE" echo "" >> "$OUT"; echo "done: written to $OUT"