4 ms·
Your post caught my eye. Since I mentioned earlier that learning perl was a good investment, thought i'd just show a quick equivalent to the awk script in your
by tux1968 13y ago
Your post caught my eye. Since I mentioned earlier that learning perl was a good investment, thought i'd just show a quick equivalent to the awk script in your blog post.
This produces same output as your awk script (except it's sorted alphabetically):
#!/usr/bin/perl -F, -an "$@"
$wins{$F[0]} += $F[6];
$losses{$F[0]} += $F[7];
END {
print "manager,total_wins,total_losses\n"
for (sort keys %wins) {
print "$_,$wins{$_},$losses{$_}\n";
}
}
EDIT: should also mention that you can automatically convert awk scripts to perl with the a2p utility which should already be installed along with perl.
- mtdewcmu 13y agoHere is another awk version of that script that's a little more concise and sorts, like the above, using newer features that are in gawk: #!/usr/local/bin/gawk -E BEGIN{ FS="," } { total_wins[$1]+=$7; total_losses[$1]+=$8; } END{ print "manager,total_wins,total_losses" n = asorti(total_wins, managers) for (i=1; i<=n; i++){ m = managers[i] print m "," total_wins[m] "," total_losses[m] } } (I recommend using gawk over mawk or nawk. You might need to install it over the awk that came with your OS.)
- hvd 13y agoThank you mtdewcmu, did not know about the built in sort in gawk, will make the switch.
- deleted 13y ago[deleted]
- hvd 13y agothank you tux1968, will consider learning perl! I did have a query on the post asking how perl would fare and you just gave that answer.