Guest User

Untitled

a guest
Jan 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # Sample usage:
  2. # $ gawk -F, -v coli=9 -v colc=10 -f calc_totals.awk < file.csv
  3.  
  4. BEGIN{
  5. # Separator for the output
  6. sep = ","
  7.  
  8. # coli is the position of the imps column.
  9. # It is passed as an arg, eg. -v coli=9
  10. if (coli > 0) {
  11. fields["imps"] = coli
  12. }
  13.  
  14. # colc is the position of the imps column.
  15. # It is passed as an arg, eg. -v coli=10
  16. if (colc > 0) {
  17. fields["clicks"] = colc
  18. }
  19.  
  20. totals["imps"] = 0
  21. totals["clicks"] = 0
  22. }
  23. {
  24. row = ""
  25. for (i = 0; i <= NF; i++){
  26. for (key in fields){
  27. if(i == fields[key]){
  28. if (key in totals) {
  29. # Add to sum
  30. totals[key] = totals[key] + $fields[key]
  31. }
  32. }
  33. }
  34. }
  35. }
  36. END{
  37. print "Impressions:", totals["imps"]
  38. print "Clicks:", totals["clicks"]
  39. }
Add Comment
Please, Sign In to add comment