Skip to content

Instantly share code, notes, and snippets.

@tvlooy
Created January 19, 2013 14:05
Show Gist options
  • Save tvlooy/4572822 to your computer and use it in GitHub Desktop.
Save tvlooy/4572822 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ ! -f "$1" ]; then
echo "Usage: $0 filename.txt"
exit -1
fi
filename=$1
months="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
linenr=0
totalhours=0
echo -e '# Tijdsregistratie\n'
while read line; do
# skip blank lines
[ -z "$line" ] && continue
(( linenr++ ))
# save fist line
if [ `expr $linenr % 2` != 0 ]; then
line1=$line
continue
fi
# print on second line
string="${months%`echo $line | cut -d' ' -f3`*}"
day=`echo $line | cut -d' ' -f2`
month=`echo "$((${#string}/4 + 1))"`
hours=`echo $line1 | cut -d']' -f1 | sed 's/\[b//' | sed 's/h//'`
job=`echo $line1 | cut -d']' -f2`
hourspoint=`echo $hours | sed 's/,/./'`
totalhours=`echo - | awk "{ print $totalhours+$hourspoint }"`
printf "%.2d-%.2d\t%s\t%s\n" $day $month "$hours" "$job"
done < $filename
echo -e "\nTotaal\t$totalhours" | sed 's/\./,/'
@Wolfr
Copy link

Wolfr commented Jan 19, 2013

That's a great script.

I hope you used OCR on the screenshots to get the original text :)

I have some extra additions to the original question so if you are interested in a small job let me know.

@tvlooy
Copy link
Author

tvlooy commented Jan 19, 2013

Sure. Stuur maar een mailtje.

@janmoesen
Copy link

Enkele ongevraagde opmerkingen/tips:

  • exit -1 is hetzelfde als exit 255 door de overflow, maar minder expliciet. Overweeg eventueel ook exit 2 ("No such file or directory".)
  • Bash ondersteunt arrays. Netter dan die stringmanipulatie: months=(Jan Feb Mar…)
  • Verkies echo $'foo' bovenecho -e 'foo'. Dat is POSIX en wordt door meer omgevingen ondersteund. Werkt overigens niet alleen met echo. Test: touch $'This is a file name\nwith two lines'.
  • Gebruik read -r om ongewenste "unescaping" te vermijden. Anders wordt C:\news in de invoer omgezet naar een "C:(nieuwe regel)ews", bijvoorbeeld.
  • "fist line"? ;-)
  • Die expr is niet nodig. Je gebruikt al ((linenr++)). Met die dubbele haakjes kan je allerlei wiskundige bewerkingen doen.

Sorry. Ik ben zó iemand.

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