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/\./,/'
@tvlooy
Copy link
Author

tvlooy commented Jan 19, 2013

Given your original file:

[tvl@orion ~/Desktop *]
$ cat dummy1.txt 
[b1,25h] Meeting initial briefing
scheduled 16 Oct 2012 from 08:00 to 12:00

[b1,5h] Voorstel Sample project
scheduled 17 Aug 2012 from 08:00 to 12:00

[b2h] Test designs Sample project
scheduled 21 Aug 2012 from 08:00 to 12:00

[b5h] Work @ Sample project
scheduled 24 Oct 2012 from 08:00 to 12:00

[b5,5h] Sample project html/css
scheduled 26 Oct 2012 from 08:00 to 12:00

[b5,75h] Sample project html/css
scheduled 29 Oct 2012 from 08:00 to 12:00

[b5h] Sample project html/css
scheduled 31 Oct 2012 from 08:00 to 12:00

[b1,25h] Sample project html/css
scheduled 1 Nov 2012 from 08:00 to 12:00

[b1,25h] Sample project html/css
scheduled 2 Nov 2012 from 08:00 to 12:00

[b0,25h] Sample project feedback
scheduled 30 Nov 2012 from 08:00 to 12:00

[b2h] Sample project vergadering
scheduled 4 Jan 2012 from 08:00 to 12:00

[b0,5h] org todos
scheduled 9 Jan 2012 from 08:00 to 12:00

[b3h] redesign homepage html/css
scheduled 16 Jan 2012 from 08:00 to 12:00

[b4h] Sample project e-mail template
scheduled 18 Jan 2012 from 08:00 to 12:00

Results in correct output:

[tvl@orion ~/Desktop *]
$ ./tijdregistratie.sh dummy1.txt 
# Tijdsregistratie

16-10   1,25    Meeting initial briefing
17-08   1,5     Voorstel Sample project
21-08   2       Test designs Sample project
24-10   5       Work @ Sample project
26-10   5,5     Sample project html/css
29-10   5,75    Sample project html/css
31-10   5       Sample project html/css
01-11   1,25    Sample project html/css
02-11   1,25    Sample project html/css
30-11   0,25    Sample project feedback
04-01   2       Sample project vergadering
09-01   0,5     org todos
16-01   3       redesign homepage html/css
18-01   4       Sample project e-mail template

Totaal  38,25

@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