fundtools

A few small scripts to help with asset allocation
git clone git://jacobedwards.org/fundtools
Log | Files | Refs | README

alloc (921B)


      1 #!/usr/bin/awk -f
      2 # Asset allocator
      3 #
      4 # Allocates the given number of assets between the list of assets
      5 # given using the percentages therein.  An example file:
      6 #
      7 # 	foo	60
      8 # 	bar	30
      9 # 	baz	10
     10 #
     11 # If given 100, it will output an identical copy of the given input,
     12 # if given 200, all the numbers will be doubled.
     13 
     14 function die(msg) {
     15 	print "error: " msg > "/dev/stderr"
     16 	status = 1
     17 	exit 1
     18 }
     19 
     20 BEGIN {
     21 	IFS = "	"
     22 	units = ARGV[1] ? ARGV[1] : 100
     23 	delete ARGV[1]
     24 	--ARGC
     25 }
     26 
     27 {
     28 	if (!NF || !/^ *[^#]/) {
     29 		print
     30 		next
     31 	}
     32 
     33 	if (NF < 2 || $2 !~ /^[0-9]+([.][0-9]+)?$/)
     34 		die($0 ": Invalid entry")
     35 
     36 	match($0, IFS);
     37 	first = substr($0, 1, RSTART + RLENGTH - 1)
     38 	value = units * $2 / 100
     39 	last = substr($0, RSTART + RLENGTH + length($2))
     40 	print first, value, last
     41 	p += $2
     42 	next
     43 }
     44 
     45 END {
     46 	if (p > 100)
     47 		die(p "%: Invalid percentage")
     48 	if (p < 100) {
     49 		print "warning: Only " p "% of assets used" > "/dev/stderr"
     50 		exit 1
     51 	}
     52 }