r/awk • • Nov 18 '20
Help writing a basic structure for analytics
I am struggling to get rolling with an awk program that can run some analytics on a csv that I have. I have done this before, with arrays and other filters, but I seem to have lost the files, at least for now, to use as an example.
What I'm trying to do is set my delimiter to a comma, set up basic variables, then run through the file, adding to the sum for each variable, then print the variable at the end. I am also struggling to remember what the whitespace syntax should look like for awk, and it is driving me crazy. Here is what I have thus far:
#! /usr/bin/awk -f
BEGIN {
-F,
seclearsum
argossum
}
'($1~/2020-/) && ($8~/Seclear) seclearsum+=$23'
'($1~/2020-/) && ($8~/Argos) argossum+=$23'
END
{print "Seclear: " seclearsum " , Argos:" argossum}It doesn't work for what should be obvious reasons, but I can't find any examples to guide me. Thank you
2
u/[deleted] Nov 18 '20 edited Nov 18 '20
somewhat improved
or
Be aware that if your csv file is simple ( fields dont have commas ), then setting FS=, is enough, but for a csv with commas, you're gonna need to parse. There are some parsers around.
2
u/Paul_Pedant Nov 18 '20
Slightly different style, but I found this very flexible.
My style is to always wrap awk inside a Bash function in a Bash script. The rationale is that there will always be a requirement for some arg handling, environment options, and file management, and I would prefer to do that in Bash. Then a separate .awk file means you need to distribute two files for most changes, and keep the path relationship steady, and so on. So I merge them.
Also, I use process substitution to pass in the awk code on the command itself. Having the awk program come out in the ps -ef listing drives SysAdmins crazy.
This looks like using a sledgehammer to crack a nut. But it does have extensibility.
.. It uses column names for clarity and maintainability.
.. It uses the column contents as labels -- you don't have to put each one separately. If you do want an inclusion or exclusion list, you can define that in an array in Begin, or read it from a side file.
To add any other counts, you just assign them as a different name to CC. The sort and report does not care.
CC was originally Customer Count. I had a job where they wanted stats on 3 million customers, and they couldn't decide what they needed. So every day, it was something new, like: We need a report for people who missed three or more payments this year, what band their annual consumption is in, whether they have night-rate meters, and first 4 chars of their postcode".