My goal is to count the sum of a column which correlates to another column in a csv.
For example, I have an input csv what looks like this
"500","my.jpg"
"500","my.jpg"
"200","another.jpg"
I want the output to be:
[{ bytes: 1000, uri: "my.jpg" }, { bytes:200, "another.jpg" }]
Note: I need to do this as a stream as there can be over 3 millions records for a given csv and looping is just too slow.
I have managed to accomplish this using awk
but I am struggling to implement it in node
Here is bash script using awk
command
awk -F, 'BEGIN { print "["}
{
gsub(/"/, ""); # Remove all quotation from csv
uri=$2; # Put the current uri in key
a[uri]++; # Increment the count of uris
b[uri] = b[uri] + $1; # total up bytes
}
END {
for (i in a) {
printf "%s{\"uri\":\"%s\",\"count\":\"%s\",\"bytes\":\"%s\"}",
separator, i, a[i], b[i]
separator = ", "
}
print "]"
}
' ./res.csv
any pointers in the right direction would be hugely appreciated