Created
January 19, 2013 14:05
-
-
Save tvlooy/4572822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/\./,/' |
Enkele ongevraagde opmerkingen/tips:
exit -1
is hetzelfde alsexit 255
door de overflow, maar minder expliciet. Overweeg eventueel ookexit 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 metecho
. Test:touch $'This is a file name\nwith two lines'
. - Gebruik
read -r
om ongewenste "unescaping" te vermijden. Anders wordtC:\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
Sure. Stuur maar een mailtje.