Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Sample usage:
- # $ gawk -F, -v coli=9 -v colc=10 -f calc_totals.awk < file.csv
- BEGIN{
- # Separator for the output
- sep = ","
- # coli is the position of the imps column.
- # It is passed as an arg, eg. -v coli=9
- if (coli > 0) {
- fields["imps"] = coli
- }
- # colc is the position of the imps column.
- # It is passed as an arg, eg. -v coli=10
- if (colc > 0) {
- fields["clicks"] = colc
- }
- totals["imps"] = 0
- totals["clicks"] = 0
- }
- {
- row = ""
- for (i = 0; i <= NF; i++){
- for (key in fields){
- if(i == fields[key]){
- if (key in totals) {
- # Add to sum
- totals[key] = totals[key] + $fields[key]
- }
- }
- }
- }
- }
- END{
- print "Impressions:", totals["imps"]
- print "Clicks:", totals["clicks"]
- }
Add Comment
Please, Sign In to add comment